c#的System.Drawing.Image内存不足异常 [英] c# Out of Memory Exception with System.Drawing.Image

查看:1477
本文介绍了c#的System.Drawing.Image内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含2张图片的pdf文件.其中一个图像是文本,另一个图像是在第一个图像之上绘制的水印.好吧,当我加载第一个图像时,一切正常,但是随后我尝试加载水印图像,并获得内存不足"异常.我有内存(打印出的内存使用量约为20MB),并且可以在计算机上打开图像(我使用的是从Google那里拿来的一张图片进行测试,直到我找不到真正的图片为止.)

I wanna create a pdf with 2 images. One of the image is a text and the other is a watermark to draw on top of the first one. Well when I load the first image everything is ok but then I try to load the watermark image and get the "Out of Memory" exception. I've got memory (printed the memory usage was like 20MB) and can open the image in my computer (I'm using one I took from google just to test until I don't get the real one).

我得到异常的代码是这样的:

The code where I get the exception is this one:

      using (System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true))
      {
        using (System.Drawing.Image imgLogo = System.Drawing.Image.FromFile(sLogoPath, true)) //This is where it throws the exception
        {
          using (Graphics gra = Graphics.FromImage(imgOriginal))
          {
            Bitmap bmLogo = new Bitmap(imgLogo);
            int nWidth = bmLogo.Size.Width;
            int nHeight = bmLogo.Size.Height;
            int nLeft = (imgOriginal.Width / 2) - (nWidth / 2);
            int nTop = (imgOriginal.Height / 2) - (nHeight / 2);
            gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
          }
          return imgOriginal;
        }
      }

我见过其他问题,例如:

I've seen the other questions like mine but:

  • 似乎没有内存问题
  • 似乎没有图像问题

你能帮我吗?谢谢:)

推荐答案

问题

您正在构建一个对象

Issue

You are building an object

  using (System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true))

然后您将其退回...但它已经被废弃了...您不需要通过使用来将其拆开来废弃该对象...任何消耗掉的东西都将需要废弃该对象.

Then you are returning it...but it is already disposed of...you need to not dispose of the object by unwrapping it with a using...whatever consumes this will need to dispose of the object.

bitmap也是内存泄漏,需要用隐式调用的usingdispose包装.

bitmap is also a memory leak and needs to be wrapped with a using or dispose called implicitly.

public System.Drawing.Image GetImage(string sOriginalPath, string sLogoPath)
{
  System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true);
  using (System.Drawing.Image imgLogo = System.Drawing.Image.FromFile(sLogoPath, true)) //This is where it throws the exception
  {
    using (Graphics gra = Graphics.FromImage(imgOriginal))
    {
      using(Bitmap bmLogo = new Bitmap(imgLogo)) 
      {
        int nWidth = bmLogo.Size.Width;
        int nHeight = bmLogo.Size.Height;
        int nLeft = (imgOriginal.Width / 2) - (nWidth / 2);
        int nTop = (imgOriginal.Height / 2) - (nHeight / 2);
        gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
      }
    }
  }
  return imgOriginal;
}


示例控制台应用程序演示

我已经测试了以下内容,并且按预期工作.


Example Console App Demo

I've tested the below and it worked as expected.

using System.Drawing;

namespace SO_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using(Image newImage = GetImage("C:\\Users\\username\\Pictures\\image.png", "C:\\Users\\username\\Pictures\\watermark.jpg"))
            {
                newImage.Save("C:\\Users\\username\\Pictures\\newImage.png");
            }
        }

        static Image GetImage(string sOriginalPath, string sLogoPath)
        {
            Image imgOriginal = Image.FromFile(sOriginalPath, true);
            using (Image imgLogo = Image.FromFile(sLogoPath, true)) //This is where it throws the exception
            {
                using (Graphics gra = Graphics.FromImage(imgOriginal))
                {
                    using (Bitmap bmLogo = new Bitmap(imgLogo))
                    {
                        int nWidth = bmLogo.Size.Width;
                        int nHeight = bmLogo.Size.Height;
                        int nLeft = (imgOriginal.Width/2) - (nWidth/2);
                        int nTop = (imgOriginal.Height/2) - (nHeight/2);
                        gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
                    }
                }
            }
            return imgOriginal;
        }
    }
}

这篇关于c#的System.Drawing.Image内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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