C # GDI + Occurrence general error detailed __c#

Source: Internet
Author: User

Generally encountered this problem friend is basically in the call Image.Save method, encountered, bitmap similar, then the cause of this problem is what?

Needless to say, apart from the normal parameter errors, permission issues, the rest of the WinForm in the case, is basically the official interpretation of the error:

Bitmap object or an Image object from a file, constructed with the file remaining locked for the object's lifetime. Therefore, you cannot change the image and save it back to produce the same file

This is the official words, then the solution is simple, since this image or when the object locked, can not complete this operation

We can take a detour, copy this object, generate a copy, and then manipulate the copy to solve the problem.

Because in GDI +, therefore, we choose graphic to do this operation:

if (picturebox_sreen.image!= null)
                {
                    Bitmap b=new Bitmap (picturebox_sreen.image.width,picturebox_ Sreen.Image.Height); Create an image file of an ideal size
                    Graphics g = graphics.fromimage (B);//Instance an object of an artboard, using the artboard g.drawimage of the image above
                    (picturebox_ Sreen.image, 0, 0);//Draw the target image on the artboard of this image file

                    b.save (sfd. FileName, System.Drawing.Imaging.ImageFormat.Jpeg);//through this image to save
                }


In this way, when saving, there will be no error, where Picturebox_screen is the PictureBox control, SFD is the File Save dialog box is resolved

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.