المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Overwrite an image



C# Programming
09-08-2009, 08:13 PM
Hi, I'm currently using Visual c# 2008 express edition. I'm trying to save an image over a file with the same file name. When it attempts to do this is bombs out because it says either it's a generic GDI+ error or the process is already in use. This makes perfect sense why it would throw those errors, but how can I overwrite the file so that it doesn't give me this problem. I've tried Opening a file stream around it then closeing it. useing the bmpPicture.Dispose(), and GC.Collect(), none of those work.Is there something else I could try? or perhaps something I'm doing wrong?

here is a snippet of my code:


private void SaveFile()
{
// Set the bitmap object to the size of the screen
bmpPicture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenShot = Graphics.FromImage(bmpPicture);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y + 25, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpPicture.Save(FilePath, ImageFormat.Bmp); //****THIS IS WHERE THE ERROR IS THROWN****
}