C#Image.Clone内存不足异常 [英] C# Image.Clone Out of Memory Exception
问题描述
所以这首先在C#中死了:
splitBitmaps.Add(neededImage.Clone(rectDimensions,neededImage.PixelFormat));
其中splitBitmaps是列表< BitMap>但是这个工作在VB中至少需要4次迭代:
arlSplitBitmaps.Add(Image.Clone(rectDimensions,Image.PixelFormat)) p>
其中arlSplitBitmaps是一个简单的数组列表。 (是的,我已经在c#中尝试过arraylist)
这是完整的:
code> 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(needsImage.Clone(rectDimensions,neededImage.PixelFormat));
}
neededImage是一个位图方式。
我无法在内网中找到任何有用的答案,特别是不是为什么它在VB中正常工作。
更新:
我实际上发现了这种工作的原因(一种),但忘了发布。它与将图像转换为位图相关,而不是仅仅是尝试克隆原始图像,如果我记得。
克隆当Rectangle中指定的坐标在位图的边界之外时,()也可能会抛出内存异常。它不会自动为您剪贴。
Why am I getting an out of memory exception?
So this dies in C# on the first time through:
splitBitmaps.Add(neededImage.Clone(rectDimensions, neededImage.PixelFormat));
Where splitBitmaps is a List<BitMap> BUT this works in VB for at least 4 iterations:
arlSplitBitmaps.Add(Image.Clone(rectDimensions, Image.PixelFormat))
Where arlSplitBitmaps is a simple array list. (And yes I've tried arraylist in c#)
This is the fullsection:
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 is a Bitmap by the way.
I can't find any useful answers on the intarweb, especially not why it works just fine in VB.
Update:
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() 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屋!