I recently encountered a strange problem when I used C # For image editing.
Because you need to modify and Save the local Image on your computer, the simplest method is to load the Image to PictureBox. After editing, you can call the Save method of the Image class to directly overwrite the original file and Save it. This method is normal on Windows 7 system and Windows 7 system with logon management permission. Later, the error "A generic error occurred in GDI +" was reported on Windows 7 system with normal logon permission. After tracking and debugging, it was determined that an error occurred when the Save method of the Image class was overwritten. Later, I changed the compromise implementation method, that is, when overwriting and saving, I first deleted the original file, and then saved the Image in the original path. This method does not display The previous GDI + error, but when you delete the original file, The following error occurs: "the process cannot access The file 'x: \ xx.jpg 'because it is being used by another process. ". The related code is as follows:
Image Src_Pic = Image.FromFile(fileName); bgBMP = new Bitmap((Image)Src_Pic.Clone()); Src_Pic.Dispose();
In this case, the original file's Image object Src_Pic has been released, but its data is only filled into the Bitmap object. In this case, the resource cannot be accessed when the corresponding file is deleted. It is estimated that the called Clone method is still associated with the original file path. (I am a newbie in C #. I am not deep enough in research. I can only guess it for the moment)
Then I found another compromise to manually implement "Clone ". Some code is as follows:
Image Src_Pic = Image.FromFile(fileName);bgBMP = new Bitmap(Src_Pic.Width, Src_Pic.Height);Graphics g = Graphics.FromImage(bgBMP);g.DrawImage(Src_Pic, new Rectangle(0, 0, Src_Pic.Width, Src_Pic.Height));Src_Pic.Dispose();
If you delete the original file this time, the file cannot be released.
The above points may be incorrect. You are welcome to discuss them.