我已經搜索並解決了此問題的解決方案。但是,我無法找到解決這個問題的方法。我使用了從Google參考中獲得的相同代碼。當在Web API中保存圖像時無法解析「GDI +中發生的通用錯誤」
這下面是我的代碼:
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;
}
錯誤:
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)
注:我已經設置了寫權限到我的文件夾
停止在ASP.NET中使用'System.Drawing'。改用SkiaSharp。 http://photosauce.net/blog/post/5-reasons-you-should-stop-using-systemdrawing-from-aspnet – user9993
如果您的Web API只是將字節從客戶端傳遞到文件,請將原始字節直接保存到文件使用流並且不需要使用任何圖像庫來浪費額外的內存。 –