C# 使用C在SQL中保存图像#
我在C#Winforms应用程序中有一个对话框。我想保存图像。但是每次我点击save按钮,都会出现一个错误 gdi中发生一般性错误+ 这是我保存图像的代码:C# 使用C在SQL中保存图像#,c#,C#,我在C#Winforms应用程序中有一个对话框。我想保存图像。但是每次我点击save按钮,都会出现一个错误 gdi中发生一般性错误+ 这是我保存图像的代码: var SavedFileName = string.Format(@"{0}.png", Guid.NewGuid()); var path = Application.StartupPath + "/passport/" + SavedFileName.ToString(); if(passportsize.Image == nul
var SavedFileName = string.Format(@"{0}.png", Guid.NewGuid());
var path = Application.StartupPath + "/passport/" + SavedFileName.ToString();
if(passportsize.Image == null)
{
SavedFileName = "";
}
else
{
passportsize.Image.Save(path,System.Drawing.Imaging.ImageFormat.Png);
}
尝试使用反斜杠而不是正斜杠。
“
\\passport
\\”而不是“/passport/”您尚未提供完整消息,因此我不能确定,但创建映像的源流可能已被处理,但映像仍与之绑定
创建映像时,应克隆它。例如:
private Image ImageFromBytes(byte[] imageBytes)
{
using (var ms = new MemoryStream(imageBytes))
{
using (var image = Image.FromStream(ms))
{
return (Image)image.Clone();
}
}
}
然后,当您稍后要保存它时,您应该不会遇到任何问题。目录存在吗?目前不存在这可能是问题所在。无关:请注意,
SavedFileName
上的.ToString
是多余的,因为它已经是一个字符串。我建议使用Path.Combine
来构建文件路径。特别是var path=path.Combine(Application.StartupPath,“passport”,SavedFileName)代码>。您使用的是Linux还是Windows?另外,应用程序。StartupPath
是保存文件的一个非常糟糕的地方;非管理员可能无法写入它,如果卸载或修复应用程序,它将被删除。将用户数据保存在中。这将无法编译。您需要通过加倍来避免反斜杠them@Hans事实上,我是在鼓励特里改进答案。谢谢@John。让他们两个都出现有点棘手。