簡體   English   中英

旋轉圖像顯示“內存不足”異常

[英]rotate image shows "Out of memory" exception

   public Bitmap rotateImage()
    {

      try
      {                      
          curImgHndl.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);

      }
      catch (Exception ex)
      {

      }

    return objCurrImageHandler.CurrentBitmap;
    }

當使用此 function 將圖像旋轉多次(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 is not supported for images with indexed pixel formats”。

如果您能提供有效且可行的旋轉、裁剪和亮度方法,那將非常有幫助。 請再次幫助。

正如 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;
}

為什么不旋轉當前的 bitmap,而不是創建副本?

public class RotationHandler 
{
   private ImageHandler imageHandler;

   public void Flip(RotateFlipType rotateFlipType)
   {
      this.imageHandler.RestorePrevious();
      this.imageHandler.CurrentBitmap.RotateFlip(rotateFlipType);
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM