亮度方法显示“内存不足".例外 [英] Brightness method shows "Out of memory" exception
本文介绍了亮度方法显示“内存不足".例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要更改c#.net 4中图像的亮度,我使用了以下方法.
To change brightness of an image in c#.net 4 i have used the following method.
public void SetBrightness(int brightness)
{
imageHandler.RestorePrevious();
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
ColorMatrix cMatrix = new ColorMatrix(CurrentColorMatrix.Array);
cMatrix.Matrix40 = cMatrix.Matrix41 = cMatrix.Matrix42 = brightness / 255.0F;
imageHandler.ProcessBitmap(cMatrix);
}
internal void ProcessBitmap(ColorMatrix colorMatrix)
{
Bitmap bmap = new Bitmap(_currentBitmap.Width, _currentBitmap.Height)
ImageAttributes imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(colorMatrix);
Graphics g = Graphics.FromImage(bmap);
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(_currentBitmap, new Rectangle(0, 0, _currentBitmap.Width,
_currentBitmap.Height), 0, 0, _currentBitmap.Width,
_currentBitmap.Height, GraphicsUnit.Pixel, imgAttributes);
_currentBitmap = (Bitmap)bmap.Clone();
}
如果亮度多次更改,则会显示内存不足"异常.我曾尝试使用使用块",但还是顺其自然.
有什么想法吗?
请查看链接
在C#中使用矩阵进行图像处理
并建议方法中是否可以进行任何类型的优化(旋转,亮度,裁剪和撤消).
If brightness is changed several times then "Out of memory" exception is shown. I have tried to use "Using block" but went in vein.
Any ideas?
please see the link
Image Processing Using Matrices in C#
and suggest if any types of optimization is possible in the methods (Rotation, brightness, crop and undo).
推荐答案
尝试处理旧的位图:每次调用ProcessBitmap时您还要再构建两个(一个以new Bitmap
开头,另一个以bmap.Clone
结尾).
在分配新值之前,请在_currentBitmap上使用Dispose,并且根本不执行Clone,因为它实际上并没有做任何有用的事情-它只需要时间和内存.
Try disposing of old bitmaps: each time you call ProcessBitmap you build two more (one with thenew Bitmap
at teh beginning, and another with thebmap.Clone
at the end).
Use Dispose on _currentBitmap before you assign the new value, and don''t do the Clone at all since it really doesn''t do anything useful - it just takes time and memory.
这篇关于亮度方法显示“内存不足".例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文