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

前端 未结 12 1521
情书的邮戳
情书的邮戳 2020-12-10 01:07

I use the following code block with some more code inside the using block:

using (System.Drawing.Bitmap tempImg =
       (System.Drawing.Bitmap)tempObj.GetDa         


        
相关标签:
12条回答
  • 2020-12-10 01:33

    Dispose your bitMap object after save image:

     bitMap.Dispose()
     oimg.Dispose()
    
     bitMap = Nothing
     oimg = Nothing
    
    0 讨论(0)
  • 2020-12-10 01:35

    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.

    0 讨论(0)
  • 2020-12-10 01:35

    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.

    0 讨论(0)
  • 2020-12-10 01:37

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

    0 讨论(0)
  • 2020-12-10 01:40

    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");
                }
            }
    
    0 讨论(0)
  • 2020-12-10 01:41

    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();
    }
    
    0 讨论(0)
提交回复
热议问题