保存图片时在GDI +中发生一般错误

时间:2019-02-26 03:53:22

标签: c# gdi+ system.drawing

我必须以post格式在byte64String请求中保存图片 当我保存该图像时,我得到A generic error occurred in GDI+

这是我的代码

byte[] ix = Convert.FromBase64String(obj.Image);

var ID = obj.Id;

using (var mStream = new MemoryStream(ix))
{
var img = Image.FromStream(mStream);

var image = obj.ImageName + ".jpg";
string path = HostingEnvironment.MapPath("/Images/" + ImageType + "/" + ID + "/" + image);
System.IO.Directory.CreateDirectory(path);


try
{
   img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);

}
catch (Exception e)
{
var d = e;
}
}

也 这不是权限问题,因为我可以在同一目录中创建文本文件

1 个答案:

答案 0 :(得分:0)

您只是在混淆路径和文件名。

如果可能会引起猜测,可能是问题所在,您可能有一个文件名作为文件夹,并且您试图保存一个同名文件,Windows禁止这样做

您的代码已调整

var image = $"{obj.ImageName }.jpg";

// get the path, and only the path
string path = HostingEnvironment.MapPath($"/Images/{ImageType}/{ID}/");

// Create directory if needed (from that path)
Directory.CreateDirectory(path,image);

...

// now create the correct full path    
var fullPath = Path.Combine(path,fileName);

// save
img.Save(fullPath, ImageFormat.Jpeg);