0

I am trying to save a JPEG image, I am getting the error : "A generic error occurred in GDI+"
I have tried the following code:

Bitmap b = new Bitmap(m_image.Width, m_image.Height);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(m_image, 0, 0, m_image.Width, m_image.Height);
g.Dispose();

Image img1 = (Image)b;
img1.Save(Filename, ImageFormat.Jpeg);
m_image.Save(Filename, ImageFormat.Jpeg);

or

m_image.save(filename, ImaheFormat.Jpeg);
kiriti
  • 35
  • 2
  • 7
  • Where does the error occur? are you disposing the bitmap? – Sayse Jun 16 '14 at 08:46
  • Are you loading the image with `Image.FromFile(Filename)` in your program? – Dirk Jun 16 '14 at 09:20
  • Yes, we have used m_image = Image.FromFile(file.FullName) in my code. Is creating any problem? – kiriti Jun 16 '14 at 09:32
  • Likely not related to your issue, but GDI+ is very picky about its resources being disposed. You have no `using` statements in you example code, it is very possible after about processing 10000 images your program will start throwing errors due to you not releasing [GDI Handles](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724291(v=vs.85).aspx). – Scott Chamberlain Jun 16 '14 at 13:36
  • possible duplicate of [A generic error occurred in GDI+, JPEG Image to MemoryStream](http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream) – Peter Ritchie Jun 16 '14 at 14:07

2 Answers2

2

According to the MSDN documention for Image.FromFile this method locks the file.

The file remains locked until the Image is disposed.

So if you use something like this

var image = Image.Load(fileName);
// ...
image.Save(fileName)

it will throw an exception because the file is still locked.

You can solve this by not loading the image using this method but rather use one of the overloads taking a Stream parameter:

Image image;
using (var stream = File.OpenRead(fileName))
{
    image = Image.FromStream(stream);
}
// ...
image.Save(fileName);

I didn't have any problems using the code above but the documentation for Image.FromStream says that

You must keep the stream open for the lifetime of the Image.

If you experience any problems you could load the file into a MemoryStream and keep that open.

Dirk
  • 10,668
  • 2
  • 35
  • 49
  • I have changed the code as you suggested, but still getting same error on this. – kiriti Jun 16 '14 at 10:08
  • @kiriti Try to save it to another filename, if that works then you know that the file you want to save it to is still locked. – Dirk Jun 16 '14 at 10:21
  • I have changed my file name and I have tried but no luck sir.. Here actuall response we are converting to byte of arrays and for image conversion we are again converting to stream and then we are save the file.Please let me know if you require any info on this.. – kiriti Jun 16 '14 at 11:48
  • I have changed to m_image = Image.FromStream(stream); but same error. – kiriti Jun 16 '14 at 12:23
  • @kiriti I'm afraid I can't help you then. If you can't save the image as any filename (only change the filename you are saving to, not the one you're loading from) then the issue is somewhere else entirely. – Dirk Jun 16 '14 at 12:53
  • thanks for help, the issue was resolved. I am getting issue because of the using block. using(MemoryStream ms = new MemoryStream(bytes)){}. After removing the using block MemoryStream ms = new MemoryStream(bytes). The issue was gone. Thanks all for helping me on this issue. – kiriti Jun 17 '14 at 05:56
  • Dirk was correct. The same issue happens with a memory stream. You cannot dispose it. – Frank Hileman Jun 17 '14 at 15:40
0

change the saving files suppose Filename = "c:\1.jpg";

img1.Save("c:\2.jpg", ImageFormat.Jpeg); m_image.Save("c:\3.jpg", ImageFormat.Jpeg); make sure that you have write permission.

I have the following example running perfect

 Image m_image = Image.FromFile(Filename);
            Bitmap b = new Bitmap(m_image.Width, m_image.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(m_image, 0, 0, m_image.Width, m_image.Height);

            Image img1 = (Image)b;
            img1.Save("C:\\1.jpg", ImageFormat.Jpeg);
            m_image.Save("C:\\2.jpg", ImageFormat.Jpeg);
Razack
  • 1,826
  • 2
  • 16
  • 37
  • I have tried with your suggestion, same issue : A generic error occurred in GDI+. Iam getting. – kiriti Jun 16 '14 at 12:43