Nag
Nag

Reputation: 509

How to convert gif images to jpeg in c# code winforms?

I am trying to convert my gif/png images to jpeg then after that i want to pass these images to sqldatabase. Because crystal report doesnot support gif images for displaying as a report.

So now i have a problem to covert gif images to jpeg.

Sample code:

data = File.ReadAllBytes(ImgPath);

data is byte[] type so i have image path now i want to convert into jpeg before ReadAllBytes() callls

I tried like this :

   using (Image img = Image.FromFile(ImgPath))
   {
             img.Save(ImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
   }

But i got Generic error Gdi something is came. thanks.

Upvotes: 0

Views: 2676

Answers (3)

Dave Elderkin
Dave Elderkin

Reputation: 1

I think you can go even simpler than bdn02's answer. Try:

Image img = Image.FromFile(@"c:\temp_10\sample.gif");
img.Save(@"c:\temp_10\sample.jpg", ImageFormat.Jpeg);

Upvotes: 0

bdn02
bdn02

Reputation: 1500

This is the code:

        byte[] imagebuffer;

        using (Image img = Image.FromFile(@"c:\temp_10\sample.gif"))
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, ImageFormat.Jpeg);
            imagebuffer = ms.ToArray();
        }

        //write to fs (if you need...)
        File.WriteAllBytes(@"c:\temp_10\sample.jpg", imagebuffer);

The namespace to use is System.Drawing

I've modified my code, i think that you don't need the file on filesystem but you can use the byte array to call Crystal report

Upvotes: 1

Parrish
Parrish

Reputation: 177

Here are two places that can help. One is Microsoft's site on how to read in image files to memory.

https://msdn.microsoft.com/en-us/library/twss4wb0(v=vs.90).aspx

The next is an example from another user on the site on how to read the file into a bite array as the format you want. I am assuming you want it as a byte array so you can do something with it rather than put it back down as a file.

How to convert image in byte array

I would have typed it all out but found the examples and they were self explanatory. :-)

Upvotes: 0

Related Questions