【发布时间】:2012-04-11 06:31:59
【问题描述】:
要在 c#.net 4 中更改图像的亮度,我使用了以下方法。
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();
}
如果多次更改亮度,则会显示“内存不足”异常。我曾尝试使用“Using block”,但很顺利。
有什么想法吗?
请看链接 http://www.codeproject.com/Articles/227016/Image-Processing-using-Matrices-in-Csharp 并建议在方法中是否可以进行任何类型的优化(旋转、亮度、裁剪和撤消)。
【问题讨论】:
-
您可能忘记在 Bitmap 对象上调用 Dispose()。它们占用大量非托管内存,垃圾收集器不会让您摆脱麻烦。
标签: c#-4.0 memory-leaks out-of-memory