C # unable to release image resources

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.