保存位图会导致通用 GDI+错误

发布于 2024-11-24 10:23:36 字数 1261 浏览 1 评论 0原文

我一直在寻找解决这个问题的方法,但还没有真正找到。 这是一些我一直在尝试使其工作但没有成功的拼凑代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

合久必婚 2024-12-01 10:23:36

通常这是权限错误。

Typically this is a permissions error.

时光是把杀猪刀 2024-12-01 10:23:36

我发现如果我将你的图像保存为 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.

随波逐流 2024-12-01 10:23:36

好吧,这是我为完成同样的事情而编写的一些代码的片段

WebClient webClient = new WebClient();
using (Stream stream = webClient.OpenRead(imgeUri))
{
   using (Bitmap bitmap = new Bitmap(stream))
   {
      stream.Flush(); // not really necessary, but doesnt hurt
      stream.Close();
      try
      {
         // now, lets save to a memory stream and
         // return the byte[]
         MemoryStream ms = new MemoryStream();

         // doesnt have to be a Png, could be whatever you want (jpb, bmp, etc.)
         bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         return ms.GetBuffer()
      }
      catch (Exception err)
      {
         Console.WriteLine("{0}\t\tError: {1}", id, err.Message);
      }
   }
}

Well, here is a snippet from some code I wrote to do the same thing

WebClient webClient = new WebClient();
using (Stream stream = webClient.OpenRead(imgeUri))
{
   using (Bitmap bitmap = new Bitmap(stream))
   {
      stream.Flush(); // not really necessary, but doesnt hurt
      stream.Close();
      try
      {
         // now, lets save to a memory stream and
         // return the byte[]
         MemoryStream ms = new MemoryStream();

         // doesnt have to be a Png, could be whatever you want (jpb, bmp, etc.)
         bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         return ms.GetBuffer()
      }
      catch (Exception err)
      {
         Console.WriteLine("{0}\t\tError: {1}", id, err.Message);
      }
   }
}
云淡月浅 2024-12-01 10:23:36

每当我遇到这个问题时,我经常发现它是由于尝试保存到无效或无法访问的路径而导致的。

在保存函数上设置断点,检查输出路径,然后尝试通过文件资源管理器访问同一路径,这是验证您没有拼写任何内容的快速方法。如果它确实存在,我会检查权限。

希望有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文