保存位图会导致通用 GDI+错误
我一直在寻找解决这个问题的方法,但还没有真正找到。 这是一些我一直在尝试使其工作但没有成功的拼凑代码。
private Image GetAlbumArt(String url)
{
byte[] rBytes;
Image Testing;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResponse = myRequest.GetResponse();
Stream rStream = myResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(rStream))
{
rBytes = br.ReadBytes(1000000);
br.Close();
}
myResponse.Close();
using (MemoryStream imageStream = new MemoryStream(rBytes, 0, rBytes.Length))
{
imageStream.Write(rBytes, 0, rBytes.Length);
Testing = Image.FromStream(imageStream, true);
Testing.Save("temp.jpg"); //Error here!
}
return Testing;
}
实际上,我宁愿不保存位图,而只是将位图返回给父函数,但这也不起作用。 (如果你想使用位图,则无法关闭内存流)
真正奇怪的是,如果我给它一个不同的 jpg url,它就可以正常工作。
不起作用:http://2.images。 napster.com/mp3s/2492/resources/207/116/files/207116316.jpg
作品: http://3.images.napster .com/mp3s/2256/resources/301/404/files/301404546.jpg
I've been searching around for a solution to this but haven't really found one.
Here is some patchwork code that I've been playing with trying to get this working but no success.
private Image GetAlbumArt(String url)
{
byte[] rBytes;
Image Testing;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResponse = myRequest.GetResponse();
Stream rStream = myResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(rStream))
{
rBytes = br.ReadBytes(1000000);
br.Close();
}
myResponse.Close();
using (MemoryStream imageStream = new MemoryStream(rBytes, 0, rBytes.Length))
{
imageStream.Write(rBytes, 0, rBytes.Length);
Testing = Image.FromStream(imageStream, true);
Testing.Save("temp.jpg"); //Error here!
}
return Testing;
}
I would actually rather not save the bitmap and just return the bitmap to the parent function, but that didnt work either. (cannot close the memory stream if you want to use the bitmap)
The really strange thing is that this works fine if i give it a different jpg url.
Doesnt work: http://2.images.napster.com/mp3s/2492/resources/207/116/files/207116316.jpg
works: http://3.images.napster.com/mp3s/2256/resources/301/404/files/301404546.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常这是权限错误。
Typically this is a permissions error.
我发现如果我将你的图像保存为 bmp,它就可以正常工作。我还检查了你的第一张图片的 DPI 比第二张图片高得多。希望这是你能经历的线索。
I found if I save your image to bmp and it works fine. I also checked the DPI of your first image is much higher than the second one. Hope this is the clue you can go through.
好吧,这是我为完成同样的事情而编写的一些代码的片段
Well, here is a snippet from some code I wrote to do the same thing
这是关于该错误的一个很好的讨论:
http://weblogs.asp.net/anasghanem/archive/2009/02/28/solving-quot-a-generic-error-occurred-in-gdi-quot-exception.aspx
Here is a good discussion on that error:
http://weblogs.asp.net/anasghanem/archive/2009/02/28/solving-quot-a-generic-error-occurred-in-gdi-quot-exception.aspx
每当我遇到这个问题时,我经常发现它是由于尝试保存到无效或无法访问的路径而导致的。
在保存函数上设置断点,检查输出路径,然后尝试通过文件资源管理器访问同一路径,这是验证您没有拼写任何内容的快速方法。如果它确实存在,我会检查权限。
希望有帮助
Whenever I experience this problem I often find it to be down to trying to save to an invalid or inaccessible path.
Sticking a breakpoint on your save function, examining the output path, and then trying to access this same path through the file explorer is a quick way to verify you haven't misspelled anything. If it does exist I would then check permissions.
Hope that helps