A Generic error occurs at GDI+ at Bitmap.Save() after using SaveFileDialog

泪湿孤枕 提交于 2019-11-28 09:39:15

Finally I could find what was wrong in my code and would like to mention it here as I think it may be useful to someone....

As I have given a relative path in tempImg.Save, and after the user clicks 'Save' in SaveFileDialog, the actual path for tempImg.Save become :

Path specified by SaveFileDialog + the relative path

automatically.

Thus if the path does not exist, this error occurs.

Thanks every one for the answers.

I also had once this problem- it happens because the bitmap locks and you can't save it( if you want I can find the exact explanation) so anyway a fix around is this: Create a new bitmap the size of the original bitmap you have- copy the original bitmap to the new created bitmap and then dispose the original bitmap and save the new one.

Bitmap bm3 = new Bitmap(bm2);

And then save.

This is usually an indicator that something else, potentially some other thread in your own application, already has the target file that you're trying to save locked at the file system level. If you look at the inner exception I believe it should mention this. If it's not directly in the InnerException Another way to confirm this (or discover what it might really be instead) is to turn on first chance exceptions in the debugger and watch for what exception is being thrown "underneath" Save and then being turned into this generic exception.

Umar Hassan

Tried all the solutions given here, but in vain. Found the solution eventually.

  1. Dispose any Graphics applied on image: g.dispose();
  2. Make sure save path exists: System.IO.Directory.Exists(dir);

Is this an ASP.NET application?

A Generic Error occured at GDI+ in asp.net mostly because of missing target folder / access permissions.

Also your code could be simplified to :

       using (Image image= dataObject.GetImage())
       {
            if (image != null)
            {
                image.Save("test.bmp");
            }
        }

A Generic error occurs at GDI+ at Bitmap.Save() is generally received when the folder is protected, or the file name is invalid.

A common error is to forget to make a name and just write the folder name alone.

In my case it was an ASP.NET application in which I replaced a single DLL, and I had to simply re-start the application pool after deployment. Then it worked fine.

This is code sample from Microsoft Forums.

// new image with transparent Alpha layer
using (var bitmap = new Bitmap(330, 18, PixelFormat.Format32bppArgb))
{
    using (var graphics = Graphics.FromImage(bitmap))
    {
        // add some anti-aliasing
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        using (var font = new Font("Arial", 14.0f, GraphicsUnit.Pixel))
        {
            using (var brush = new SolidBrush(Color.White))
            {
                // draw it
                graphics.DrawString(user.Email, font, brush, 0, 0);
            }
        }
    }

    // setup the response
    Response.Clear();
    Response.ContentType = "image/png";
    Response.BufferOutput = true;

    // write it to the output stream
    bitmap.Save(Response.OutputStream, ImageFormat.Png);
    Response.Flush();
}
RoopzD

I am trying to save image from resource and it gives me too GDI error when I directly use the method Bitmap.Save(filepath). I think We can use the same below code for any other bitmap image by cloning it.

Private void SaveResourceImage() {
    object resBmpObject = Resource.Image1.Clone();//Bitmap Image from resource file
    //object resBmpObject = anyBmpImage.clone(); //for image other than resource image
    Bitmap resBmpImage = (Bitmap)resBmpObject;
    resBmpImage.Save(destFilePath, System.Drawing.Imaging.ImageFormat.Png);
    resBmpImage.dispose();
}
Lokesh Yadav

Dispose your bitMap object after save image:

 bitMap.Dispose()
 oimg.Dispose()

 bitMap = Nothing
 oimg = Nothing

In my case, i was saving the bitmap file on the same location as the source, So that's the problem. I save the bitmap to the new location and all fine now.

I was facing the same issue, by changing image type ".bmp" to ".png" its work form me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!