1
votes

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
Stop using System.Drawing inside ASP.NET. Use SkiaSharp instead. photosauce.net/blog/post/… - user9993
If your Web API is just passing the bytes from client to files, save raw bytes directly to file using streams and no need to use any imaging libraries to waste extra memory. - Lex Li

1 Answers

3
votes

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).