从服务器获取位图时内存不足?

发布于 2024-08-28 15:52:37 字数 738 浏览 7 评论 0原文

我正在制作一个使用许多图像的应用程序。该应用程序从服务器获取图像,并一次下载一张。 在许多图像之后,位图的创建返回异常,但我不知道如何解决这个问题。这是我下载图像的功能:

 public static Bitmap getImageFromWholeURL(String sURL)
    {

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(sURL);
        myRequest.Method = "GET";


        // If it does not exist anything on the url, then return null
        try
        {
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();
            return bmp;
        }
        catch (Exception e)
        {
            return null;
        }

      }

有人可以帮我吗? 提前致谢!

I'm making an application which uses MANY images. The application gets the images from a server, and downloads them one at a time.
After many images the creation of a bitmap returns an exception, but i don't know how to solve this. Here is my function for downloading the images:

 public static Bitmap getImageFromWholeURL(String sURL)
    {

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(sURL);
        myRequest.Method = "GET";


        // If it does not exist anything on the url, then return null
        try
        {
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();
            return bmp;
        }
        catch (Exception e)
        {
            return null;
        }

      }

Can anyone help me out here?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

羅雙樹 2024-09-04 15:52:37

将响应传输到磁盘而不是将其保存在内存中。然后保留有关已保存到临时位置的图像的信息,而不是图像本身。

Stream that response to disk rather than keep it in memory. Then keep around the information about the image you've saved to a temporary place instead of the image itself.

一城柳絮吹成雪 2024-09-04 15:52:37

如果您在图片框中显示所有这些(并且根据您的评论,我认为您是)那么您应该处理旧图像(此博客条目 有助于解释它):

if(myPictureBox.Image != null)
{
    myPictureBox.Image.Dispose();
}
myPictureBox.Image = getImageFromWholeURL(url);

作为风格的旁注,方法名称应该是 PascalCase,而不是驼峰式命名,我会丢失匈牙利语关于参数的概念。

If you're showing these all in a Picturebox (and based on your comments I think you are) then you should Dispose of the old images (this blog entry helps explain it):

if(myPictureBox.Image != null)
{
    myPictureBox.Image.Dispose();
}
myPictureBox.Image = getImageFromWholeURL(url);

As a side note on style, method names are supposed to be PascalCase, not camelCase and I'd lose the hungarian notion on the parameter.

黎歌 2024-09-04 15:52:37

“许多图像”当然与内存不足密切相关。位图可能会变得很大,它们会占用大量非托管虚拟内存。您必须使您的程序更加智能并在内存中存储更少的位图。或者将它们暂时保存到文件中。或者如有必要,重新下载它们。并使用 Dispose() 方法正确清理其资源,这对于 Bitmap 类尤其重要。

"Many images" is of course closely associated with running out of memory. Bitmaps can get large, they'll eat up a lot of unmanaged virtual memory. You'll have to make your program smarter and store less bitmaps in memory. Or save them temporarily to a file. Or re-download them if necessary. And properly clean up their resources with the Dispose() method, especially important for the Bitmap class.

耳钉梦 2024-09-04 15:52:37

您如何处理 System.Drawing.Bitmap 对象?你都把它们都记在心里了吗?那么在某个时候你不可避免地会遇到内存不足的异常。

根据您的需要,您应该在某个时候丢弃图像。如果您确实需要它们,请将它们存储在闪存中的文件中。另外,尝试使用较小的文件。

What do you do with the System.Drawing.Bitmap objects? Do you keep all of them in memory? Then it is inevitable that you'll get an out of memory exception at some point.

Based on your needs you should discard the images at some point. If you do need them, store them in a file in flash. Also, try using files of smaller sizes.

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