The System. Drawing. Image. Save method causes a general error in "GDI +"

Source: Internet
Author: User

In the development of. NET applications, using the system. Drawing. image. Save method causes a general error in "GDI +". There are usually three reasons:
1. The account has no write permission.
Solution: grant the network service account write permission.
2. The specified physical path does not exist.
Solution:
Before calling the Save method, first determine whether the directory exists. If it does not exist, it is created.
If (! Directory. exists (dirpath ))
Directory. createdirectory (dirpath );
3. The saved file already exists and is locked for some reason.
Solution:
Restart IIS to unlock it. Use the using statement in the code to release all resources used by the image object.

My situation:
Open the image file with openfiledialog, and then use savefiledialog to save the file.A general error occurs in GDI +.", I was thinking that the opened file has not been released, so I used openfiledialog1.dispose () to release it, but it was not successful. After an image is opened from a memorysream instance, the stream is immediately closed. This error occurs in image. Save. For a long time, I have been encountering people with the same questions as me. The comrades above csdn have not provided a practical answer. Finally, I finally found the answer on Microsoft's website: (The following is an official solution)
Symptom
A Bitmap object or an image object is constructed from a file. The object remains locked for the lifetime of the object. Therefore, you cannot change the image and save it back to generate the same file.

Alternative Method
• Create a non-index image.
• Create an index image.
In both cases, the bitmap. Dispose () method is called on the original bitmap to delete the lock or delete the file. The stream or memory is active.

Create a non-indexed Image
Even if the original image is indexed in the format, This method requires that the new image be in a non-indexed pixel format that is located at each pixel (more than 8 bits. This work und uses the graphics. drawimage () method to copy an image to a new bitmap object:
1. Construct the original bitmap of the slave stream, slave memory, or slave file.
2. Create a New bitmap of the same size, with more than 8-pixel (BPP) per pixel format.
3. Use the graphics. fromimage () method to obtain the two-bitmap graphics object.
4. Used for graphics. drawimage () to draw the first bitmap to the second bitmap.
5. Used for graphics. Dispose () disposal is a graph.
6. bitmap. Dispose () is the first bitmap disposal.

Create index image
This solution creates a bitmap object in the index format:
1. Construct the original bitmap of the slave stream, slave memory, or slave file.
2. Create a New bitmap with the same size and pixel format as the first bitmap.
3. Use the Bitmap. LockBits () method to lock the entire image. For two Bitmap objects, use their native pixel format.
4. Use the Marshal. Copy function or other memory Copy function to Copy the first bitmap to the second bitmap.
5. Use the Bitmap. UnlockBits () method to unlock two Bitmap objects.
6. Bitmap. Dispose () is the first Bitmap disposal.
Because the foreign thinking is different from ours, I will use an example to explain how to create a non-indexed image here.
Private void ToolStripMenuItem_Click (object sender, EventArgs e)
{
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
// Create a bitmap type bmp variable to read the file.
Bitmap BMP = new Bitmap (openfiledialog1. filename );
// Create the second bitmap type BMP 2 variable. Here I set it based on my program needs.
Bitmap BMP 2 = new Bitmap (1024,768, pixelformat. format16bpprgb555 );
// Copy the first BMP to BMP 2
Graphics draw = graphics. fromimage (BMP 2 );
Draw. drawimage (BMP, 0, 0 );
PictureBox1.Image = (Image) BMP 2; // read BMP 2 to picturebox
FILE = openFileDialog1.FileName;
OpenFileDialog1.Dispose ();
Draw. Dispose ();
Bmp. Dispose (); // release bmp file resources
}
}
After reading the file above, no error will occur during saving.

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.