你如何重新使用在C#中的对象相同的内存? (尤其是位图) [英] How do you reuse the same memory from an object in C#? (Bitmap in particular)

查看:97
本文介绍了你如何重新使用在C#中的对象相同的内存? (尤其是位图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的图像缩放程序。通过拖动多个文件到.exe文件,它会经过和调整每个文件。它的工作原理达到一定地步的OOM(内存不足)的异常被抛出。我试过呼吁位图和处置其设置为空,但也似乎做任何事情。

I'm writing a simple image resizing program. By dragging multiple files onto the .exe, it will go through and resize each file. It works up to a certain point where an OOM (out of memory) exception is being thrown. I've tried calling Dispose on the bitmap and setting it to Null, but neither seems to do anything.

Bitmap current_image;
for (int i = 0; i < imagesfilepath.Count; ++i)
        {
            // Load the image. 
            if ( current_image != Null )
            {
                current_image.Dispose();
                current_image = Null;
            }
            current_image = (Bitmap)Image.FromFile(imagesfilepath[i], true);

            // Resize it.
            // Save it.
        }

1.5 GB的已使用后,该例外被抛出一般。我可以解决这个问题,通过限制图像的用户可以在同一时间调整数额,但我不应该能够公正分配内存1位图,然后重新使用每次迭代?

The exception is generally thrown after 1.5 GB has been used. I can get around this issue by limiting the amount of images a user can resize at one time, but shouldn't I be able to just allocate memory for 1 Bitmap, and reuse it every iteration?

推荐答案

Image.FromFile()抛出OutOfMemoryException异常当文件是不是有效的图像:

Image.FromFile() throws OutOfMemoryException when the file is not a valid image:

异常条件
  OutOfMemoryException异常结果
  该文件没有有效的图像格式。
  -要么-
  GDI +不支持的文件的像素格式。

Exception Condition OutOfMemoryException
The file does not have a valid image format. -or- GDI+ does not support the pixel format of the file.

是的,这是没有意义的,是混乱的,但它是什么。

Yes, this makes no sense and is confusing, but it is what it is.

MSDN: Image.FromFile

这篇关于你如何重新使用在C#中的对象相同的内存? (尤其是位图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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