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