Click here to Skip to main content
14,211,493 members
Rate this:
Please Sign up or sign in to 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 16-Jan-10 12:20pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
Rate this:
Please Sign up or sign in to vote.

Solution 3

Sure - that means that the image that was being held open, was the one that you were working with, which is not surprising.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
Rate this:
Please Sign up or sign in to vote.

Solution 5

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!
Rate this:
Please Sign up or sign in to vote.

Solution 6

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)



Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 10 Apr 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100