GDI +中出现一般错误 [英] A Generic error occured in GDI+

查看:87
本文介绍了GDI +中出现一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



这是我的代码。



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.

推荐答案

我在这里遇到一些困难,也许你可以解释一下:



1)你在这里使用一个字符串变量来替换另一个字符串。可以更容易地实现相同的结果
I'm seeing some difficulties here, maybe you can explain them:

1) You're using a string variable here to replace itself with another string. The same result could be achieved much easier
sFileName = Bigimg.FileName.Replace(Bigimg.FileName.ToString(), news.NewsId.ToString()) + sExtension;

//

sFileName = news.NewsId.ToString() + sExtension;





2)名为BlockImagePath的东西应该是一个字符串。所以不需要ToString()它。

并使用 Path.Combine [ ^ ]可以帮助路径创建健壮性。



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.


I将转换后的图像保存为另一个名称。现在它工作正常。



System.Drawing.Image img = System.Drawing.Image.FromFile(@sPath);

sPath1 = ConfigurationManager.AppSettings [BlockImagePath]。ToString();

replacementname = Path.GetFileNameWithoutExtension(sPath);

img.Save( @ sPath1 + replacementname +1.jpg,System.Drawing.Imaging.ImageFormat.Jpeg);



谢谢朋友们
I saved the converted image in another name.now it works fine.

System.Drawing.Image img = System.Drawing.Image.FromFile(@sPath);
sPath1 = ConfigurationManager.AppSettings["BlockImagePath"].ToString();
replacedname = Path.GetFileNameWithoutExtension(sPath);
img.Save(@sPath1 + replacedname + "1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Thanks friends


这篇关于GDI +中出现一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆