问题描述
嗨朋友们,
这是我的代码。
Hi friends,
This is My code.
sExtension = Path.GetExtension(Bigimg.FileName);
sFileName = Bigimg.FileName.Replace(Bigimg.FileName.ToString(), news.NewsId.ToString()) + sExtension;
string sPath1 = ConfigurationManager.AppSettings["BlockImagePath"].ToString() + sFileName;
if (!File.Exists(sPath1))
File.Delete(sPath1);
Bigimg.PostedFile.SaveAs(sPath1);
System.Drawing.Image img = System.Drawing.Image.FromFile(sPath1);
img.Save(@sPath1, System.Drawing.Imaging.ImageFormat.Jpeg);
我使用上面的代码将图像格式转换为JPEG。
最后一行
, img.save
抛出异常GDI +中出现一般错误
请帮我解决这个问题。
谢谢。
I using the above code to convert image format to JPEG.
in last line, img.save
thrown exception "A Generic error occured in GDI+"
Please help me to resolve this.
Thanks.
推荐答案
sFileName = Bigimg.FileName.Replace(Bigimg.FileName.ToString(), news.NewsId.ToString()) + sExtension;
//
sFileName = news.NewsId.ToString() + sExtension;
2)名为BlockImagePath的东西应该是一个字符串。所以不需要ToString()它。
并使用 []可以帮助路径创建健壮性。
2) Something called "BlockImagePath" should be a string already. So no need to ToString() it.
And using Path.Combine[^] can help path creation robustness.
string sPath1 = Path.Combine(
ConfigurationManager.AppSettings["BlockImagePath"],
sFileName
);
3)删除不存在的内容?
3) Delete something that isn't there?
if (!File.Exists(sPath1))
File.Delete(sPath1);
我想你没有!就想要那个。
4)你将图像保存到磁盘,再次读取,然后重新写入。
我认为你应该提供对 PostedFile 的数据类型的一些见解。
I suppose that you wanted that without the "!".
4) And the you're saving an image to disk, read it again, and re-write it another time.
I think you should provide some insight in the data type of PostedFile.
这篇关于GDI +中出现一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!