【问题标题】:A generic error occured in GDI+ while saving image保存图像时 GDI+ 中发生一般错误
【发布时间】:2019-02-26 03:53:22
【问题描述】:

我必须以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;
}
}

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

【问题讨论】:

  • 使用File.WriteAllBytes 将字节写入文件,并查看其中是否有有效图像,我怀疑它不是您认为的那样。还要格式化你的代码,它不可读
  • 是的,有一张有效的图片

标签: c# gdi+ system.drawing


【解决方案1】:

很简单,您混淆了路径和文件名。

这个问题可能会引起猜测,您可能有一个文件夹是您的文件名,并且您正在尝试保存一个具有相同名称的文件,这是 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);

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 2015-07-02
    • 2019-01-02
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    相关资源
    最近更新 更多