0

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.

Nag
  • 489
  • 3
  • 6
  • 27

3 Answers3

1

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

bdn02
  • 1,500
  • 9
  • 15
0

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. :-)

Community
  • 1
  • 1
Parrish
  • 159
  • 5
0

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);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135