要更改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();
}
如果亮度多次改變,則會顯示「內存不足」異常。我曾嘗試使用「使用塊」,但仍然堅持下去。
任何想法?
請參閱鏈接 http://www.codeproject.com/Articles/227016/Image-Processing-using-Matrices-in-Csharp 並建議在方法(旋轉,亮度,裁剪和撤消)中是否可以進行任何類型的優化。
你可能忘記對位圖對象調用Dispose()。他們需要大量的非託管內存,垃圾收集器不會讓你擺脫麻煩。 – 2012-04-11 10:09:16