public Bitmap rotateImage()
{
try
{
curImgHndl.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);
}
catch (Exception ex)
{
}
return objCurrImageHandler.CurrentBitmap;
}
使用此功能将图像旋转几次(5次或更多次)时,它将显示错误消息
“记不清” 。
为了在c#.net 4中批准图像,我使用了ImageFunctions.dll。反编译dll我得到以下内容。
给出了整个代码中仅一部分用于旋转
public class RotationHandler
{
private ImageHandler imageHandler;
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
bitmap.RotateFlip(rotateFlipType);
this.imageHandler.CurrentBitmap = (Bitmap) bitmap.Clone();
}
}
我该如何解决?
下面的方法解决了lazyberezovsky建议的问题。
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
this.imageHandler.CurrentBitmap.RotateFlip(rotateFlipType);
}
但是亮度方法还有另一个问题。
public void SetBrightness(int brightness)
{
Bitmap temp = (Bitmap)_currentBitmap;
Bitmap bmap = (Bitmap)temp.Clone();
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j, Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
_currentBitmap = (Bitmap)bmap.Clone();
}
此方法适用于某些图像,不适用于其他图像,并显示以下错误
“具有索引像素格式的图像不支持SetPixel。”
如果您可以提供有效且可行的旋转,修剪和亮度方法,将非常有帮助。
请再次帮助。
最佳答案
正如Claudio所提到的,您需要确保处置所有未使用的Bitmaps
。
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
this.imageHandler.CurrentBitmap.Dispose(); // dispose of current bitmap
bitmap.RotateFlip(rotateFlipType);
Bitmap clone_map = (Bitmap) bitmap.Clone();
bitmap.Dipose(); // dispose of original cloned Bitmap
this.imageHandler.CurrentBitmap = clone_map;
}
您可能可以简化为:
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
this.imageHandler.CurrentBitmap.Dispose(); // dispose of current bitmap
bitmap.RotateFlip(rotateFlipType);
this.imageHandler.CurrentBitmap = bitmap;
}
关于c# - 旋转图像显示“内存不足”异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046917/