Go Generic error occurred in GDI +

Source: Internet
Author: User

In development. NET applications, the use of the System.Drawing.Image.Save method causes "generic errors in GDI +" to occur, usually for the following three reasons:
1. The corresponding account does not have write permission.
Workaround: Give the NETWORK SERVICE account write permissions.
2. The specified physical path does not exist.
Workaround:
Before calling the Save method, determine if the directory exists and if it does not exist, create it.
if (! Directory.Exists (Dirpath))
Directory.CreateDirectory (Dirpath);
3. The saved file already exists and is locked for some reason.
Workaround:
Restart IIS to unlock the lock. and use the using statement in your code to ensure that all resources used by the Image object are disposed.

The situation I encountered:
When you open the picture file with OpenFileDialog and then save the file with SaveFileDialog, it appears "generic error occurred in GDI +"I thought that the open file had not been released, so I used Openfiledialog1.dispose () to release it, but there was no success. Once an image is opened from an Memorysream instance, the stream is immediately closed, and the error occurs when Image.Save. I "Ferry" for a long time are met with me the same problem, csdn above the comrades did not give a practical answer. Finally, we finally found the answer on Microsoft's website: (The official solution below)
Symptoms
Bitmap an object or an image object from a file that is constructed while the file remains locked for the lifetime of the object. Therefore, the image cannot be changed and saved back to it to produce the same file.

Alternative methods
• Create a non-indexed image.
• Create an indexed image.
In both cases, the original bitmap is called on the Bitmap.dispose () method to delete the file lock or delete requirement, stream or memory hold activity.

Creating a non-indexed image
Even though the original image is indexed in the format, the method requires that the new image be located in a pixel (more than 8 bits-)-, non-indexed pixel format. This workaround uses the Graphics.DrawImage () method to copy the image to the new Bitmap object:
1. Construct the original bitmap from the stream, from memory, or from the file.
2. Create a new bitmap of the same size with a more than 8-bit-pixel (BPP) per pixel format.
3. Use the Graphics.fromimage () method to obtain information about the two-bit graphics object.
4. For Graphics.DrawImage () draw first graph to two-bit graph.
5. For graphics.dispose () disposition is graphics.
6. For Bitmap.dispose () is the first figure disposition.

Creating an indexed image
This workaround creates a Bitmap object in the indexed format:
1. Construct the original bitmap from the stream, from memory, or from the file.
2. Create a new bitmap with the same size and pixel format as the first diagram.
3. Use the Bitmap.lockbits () method to lock the entire image for two Bitmap objects in their native pixel format.
4. Use the Marshal.Copy function or other memory copy function to copy from the first diagram to the two-bit graph image bit.
5. Use the Bitmap.unlockbits () method to unlock two Bitmap objects.
6. For Bitmap.dispose () is the first figure disposition.
Since the thinking of foreigners is different from ours, I use the example to explain that I am using to create a non-indexed image.
private void Toolstripmenuitem_click (object sender, EventArgs e)
{
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
Create a BMP variable of type bitmap to read the file.
Bitmap bmp = New Bitmap (openFileDialog1. FileName);
Create a second bitmap type of BMP2 variable, I am here according to my program needs to set.
Bitmap bmp2 = new Bitmap (1024x768, 768, pixelformat.format16bpprgb555);
Copy the first BMP into the BMP2
Graphics draw = Graphics.fromimage (BMP2);
Draw. DrawImage (bmp,0,0);
pictureBox1.Image = (Image) bmp2;//Read BMP2 to PictureBox
FILE = Openfiledialog1.filename;
Openfiledialog1.dispose ();
Draw. Dispose ();
Bmp. Dispose ();//Release BMP file Resource
}
}
With the above reading the file, there will be no error at the time of saving.

Original link: generic error in GDI +

Go Generic error occurred in GDI +

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.