C# Image.Clone 内存不足异常 [英] C# Image.Clone Out of Memory Exception

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

问题描述

为什么会出现内存不足异常?

Why am I getting an out of memory exception?

所以这在 C# 中第一次通过:

So this dies in C# on the first time through:

splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat));

其中 splitBitmaps 是一个 List但是这在 VB 中至少可以进行 4 次迭代:

Where splitBitmaps is a List<BitMap> BUT this works in VB for at least 4 iterations:

arlSplitBitmaps.Add(Image.Clone(rectDimensions, Image.PixelFormat))

其中 arlSplitBitmaps 是一个简单的数组列表.(是的,我在 C# 中尝试过 arraylist)

Where arlSplitBitmaps is a simple array list. (And yes I've tried arraylist in c#)

这是全文:

for (Int32 splitIndex = 0; splitIndex <= numberOfResultingImages - 1; splitIndex++)
{ 
  Rectangle rectDimensions;

  if (splitIndex < numberOfResultingImages - 1) 
  {
    rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
      splitImageWidth, splitImageHeight); 
  } 
  else 
  {
    rectDimensions = new Rectangle(splitImageWidth * splitIndex, 0, 
     sourceImageWidth - (splitImageWidth * splitIndex), splitImageHeight); 
  } 

  splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat)); 

}

neededImage 顺便说一下是位图.

neededImage is a Bitmap by the way.

我在 intarweb 上找不到任何有用的答案,尤其是为什么它在 VB 中工作得很好.

I can't find any useful answers on the intarweb, especially not why it works just fine in VB.

更新:

我实际上为这项工作找到了一个原因(有点),但忘记发布了.如果我记得,它与将图像转换为位图有关,而不是仅仅尝试克隆原始图像.

I actually found a reason (sort of) for this working but forgot to post it. It has to do with converting the image to a bitmap instead of just trying to clone the raw image if I remember.

推荐答案

Clone() 也可能在 Rectangle 中指定的坐标超出位图边界时抛出 Out of memory 异常.它不会自动为您剪辑它们.

Clone() may also throw an Out of memory exception when the coordinates specified in the Rectangle are outside the bounds of the bitmap. It will not clip them automatically for you.

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

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