使用graphic.DrawImage获取内存异常 [英] Getting Out of Memory exception with graphic.DrawImage

查看:71
本文介绍了使用graphic.DrawImage获取内存异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp:FileUpload(fuInsertImage1),我允许用户上传要调整大小并保存到服务器的图像。我使用InputStream抓取图像并将其存储在Bitmap中。然后我将位图传递给Resize_Image方法。这适用于当地新闻公司,因此图像质量很高,通常为15Mb或更大。请参阅下面的代码。



I have an asp:FileUpload (fuInsertImage1) that I''m allowing a user to upload an image to be resized and saved to a server. I grab the image using an InputStream and store it in a Bitmap. I then pass the bitmap into a Resize_Image method. This is for a local news company so the images are high quality, usually 15Mb or larger in size. See code below.

Bitmap bmp;
using(Bitmap temp = System.Drawing.Image.FromStream(fuInsertImage1.PostedFile.InputStream))
bmp = resize_image(temp);
bmp.Save(directory);
bmp.Dispose();

protected Bitmap resize_image(Bitmap img)
{
 //determines if image width is larger that 1024 pixels
 //if yes, resizes image to 1024 width and keeps aspect ratio for height.
 int width, height;
 float percentage = 1; //This is to keep the aspect ratio of the image
 width = img.Width;
 height = img.Height;

 if (width > 1024)
  {
   percentage = (float)1024 / width;
   Bitmap bmp = new Bitmap((int)Math.Round(width * percentage, 0), (int)Math.Round(height * percentage, 0));
   Graphics graphic = Graphics.FromImage((System.Drawing.Image)bmp);

    try
    {
     graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
     graphic.DrawImage(img, 0, 0, (int)Math.Round(width * percentage), (int)Math.Round(height * percentage)); //This is where the exception is thrown.  

     return bmp;
    }
     
    catch (Exception ex)
    {
     return null;
    }

    finally
    {
     bmp.Dispose();
     graphic.Dispose();
    }
   }

   else
   {
    return img;
   }
  }





任何人都可以帮我弄清楚为什么抛出这个异常或指向另一个异常,更有效率,方向。我的开发环境中也有4GB的Ram,所以我不认为这是物理内存问题。任何帮助将不胜感激。



Can anyone help me figure out why this exception is being thrown or point me in another, more efficient, direction. I have 4GB of Ram in my dev environment also, so I don''t think it''s a physical memory issue. Any help would be greatly appreciated.

推荐答案

在不知道任何有关图像的情况下,就像它的像素尺寸一样,它很难被钉上


GDI中的某些操作会抛出一个OutOfMemory异常,即使他认为这个问题与RAM耗尽没有任何关系。例如,.NET中的任何对象,无论机器中有多少内存,都可以大于2GB。应用程序泄漏句柄可能导致应用程序抛出OutOfMemoryException,即使有大量可用内存。这可能是因为当你完成它们时没有正确释放各种GDI对象,比如分配一个新的Brush或Pen或Graphics,或者......,对象,当你完成时不调用Dispose 。这样做足够了,你的应用程序不仅会崩溃,甚至可能会崩溃。
Without knowing anything about the image in question, like it''s dimensions in pixels, it''s pretty hard to nail down.

Some operations in GDI will throw an OutOfMemory exception, even thought he problem doesn''t have anything to do with running out of RAM. For example, no object in .NET, no matter how much memory is in the machine, can be larger than 2GB is size. An app leaking handles can cause an app to throw an OutOfMemoryException, even though there''s tons of memory available. This can be caused when various GDI objects are not released properly when you''re done with them, such as allocating a new Brush or Pen or Graphics, or ..., object and not calling Dispose on them when you''re done. Do this enough and your app will not only crash itself, but possibly even Windows.


这篇关于使用graphic.DrawImage获取内存异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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