Unable to resolve “A generic error occurred in GDI

2019-08-26 00:57发布

I have googled and searched solution for this cause. But, I cannot able to find the solution for this problem. I have used the same code that I got from google references.

Here below is my code:

public HttpResponseMessage Save_ScreenCaptureDetails([FromBody] ScreenCapture objScreencapture)
{
    //Save the image in given path
    var img2 = Base64ToImage(objScreencapture.FulImg64);
    using (var screenImage = new Bitmap(img2))
    {
        screenImage.Save(
            (string.Format(@"{0}", objScreencapture.FullImagepath)), ImageFormat.Png);
    }

    var thumbnail = Base64ToImage(objScreencapture.ThumbImg64);
    using (var thumbImage = new Bitmap(thumbnail))
    {
        thumbImage.Save(
         (string.Format(@"{0}", objScreencapture.ThumbImagepath)), ImageFormat.Png);
    }

    var result = IreportServices.Save_ScreenCaptureDetails(objScreencapture);
    return Request.CreateResponse(result == null ? HttpStatusCode.NoContent : HttpStatusCode.OK, result);
}

public Image Base64ToImage(string base64String)
{
    // Convert Base64 String to byte[]
    var imageBytes = Convert.FromBase64String(base64String);
    var ms = new MemoryStream(imageBytes, 0,
      imageBytes.Length);

    // Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length);
    var image = Image.FromStream(ms, true);
    return image;
}

Error:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format)

Note: I have already set the write permission to my folder

1条回答
时光不老,我们不散
2楼-- · 2019-08-26 01:37

There are some possible reason for this problem.

  • You need to check whether current user have (IIS user) write permission on your destination folder.
  • You need to check whether current file path objScreencapture.ThumbImagepath and/or objScreencapture.FullImagepath exist or not (except file name).
查看更多
登录 后发表回答