Click here to Skip to main content
15,391,989 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This code is giving me problems:
        <br />
private void SaveBMP(Bitmap bmp)<br />
        {<br />
            bmp.Save(_project.MapFilePath);<br />
        }

This line throws a generic GDI+ error.
bmp.Save(_project.MapFilePath);

Any ideas?

quote:
It means that the file path points to an image that has already been opened in GDI+. You need to be careful to close your images when you are done with them. There's tons of info on this on the web, but that's the core of all the info you'll find. Bugs in GDI+ cause it to hold a file open, unless you're very careful in how you use them.

How do I close a bitmap image?
Posted
Updated 18-Sep-21 23:01pm
v2

It means that the file path points to an image that has already been opened in GDI+. You need to be careful to close your images when you are done with them. There's tons of info on this on the web, but that's the core of all the info you'll find. Bugs in GDI+ cause it to hold a file open, unless you're very careful in how you use them.

You close it by calling it's Dispose method. Anything that has a Dispose method, you should call when you finish using it. I typically write a helper class that loads an image, makes a copy, closes the original and returns the copy, so that I never have this problem.
   
v2
Sure - that means that the image that was being held open, was the one that you were working with, which is not surprising.
   
The solution was to do:

private void SaveBMP(ref Bitmap bmp) // now 'ref' parameter
{
    try    
    {       
        bmp.Save(_project.MapFilePath);    
    }    
    catch    
    {       
        Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat);
        Graphics g = Graphics.FromImage(bitmap);
        g.DrawImage(bmp, new Point(0,0));
        g.Dispose();        
        bmp.Dispose();       
        bitmap.Save(_project.MapFilePath);
        bmp = bitmap; // preserve clone        
    }
}


Thank you for the help!
   
v2
C#
public static void SaveJpeg
(string path, Image img, int quality)
{
EncoderParameter qualityParam
= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

ImageCodecInfo jpegCodec
= GetEncoderInfo(@"image/jpeg");

EncoderParameters encoderParams
= new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

System.IO.MemoryStream mss = new System.IO.MemoryStream();

System.IO.FileStream fs
= new System.IO.FileStream(path, System.IO.FileMode.Create
, System.IO.FileAccess.ReadWrite);

img.Save(mss, jpegCodec, encoderParams);
byte[] matriz = mss.ToArray();
fs.Write(matriz, 0, matriz.Length);

mss.Close();
fs.Close();
}
   
Comments
CHill60 16-Apr-14 13:32pm
   
Problem was resolved 4 years ago!
C#
Bitmap newBmp = new Bitmap(bmp);
bmp.Dispose();
newBmp.Save("yourPath");
MessageBox.Show("Saved");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900