2012-04-16 64 views
0
public class ImageHandler 
{ 
    private Bitmap _currentBitmap; 
    private Bitmap _bitmapbeforeProcessing; 

    public Bitmap CurrentBitmap 
    { 
     get 
     { 
      if (_currentBitmap == null) 
      { 
       _currentBitmap = new Bitmap(1, 1); 
      } 
      return _currentBitmap; 
     } 
     set { _currentBitmap = value; } 
    } 

    public string CurrentBitmapPath { get; set; } 

    public void ResetBitmap() 
    { 
     if (_currentBitmap != null && _bitmapbeforeProcessing != null) 
     { 
      Bitmap temp = (Bitmap)_currentBitmap.Clone(); 
      _currentBitmap = (Bitmap)_bitmapbeforeProcessing.Clone(); 
      _bitmapbeforeProcessing = (Bitmap)temp.Clone(); 
     } 
    } 


    internal void RestorePrevious() 
    { 
     _bitmapbeforeProcessing = _currentBitmap; 
    } 

} 

public class RotationHandler 
    { 
    private ImageHandler imageHandler; 


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

} 

當ResetBitmap()在循環後調用時,它顯示「參數無效」。但如果在c#中撤消圖像

this.imageHandler.CurrentBitmap.Dispose();被評論,然後正常工作。但是如果多次調用Flip()方法,則會顯示「內存不足」異常。

我該如何解決?

回答

0

雖然Bitmap是一個C#對象,但它確實是一個win32對象,因此,您必須在完成它時調用Dispose()。

你正在做的:

_CurrentBitmap = _CurrentBitmap.Clone(); 

當你應該做的:

_Temp = _CurrentBitmap.Clone(); 
_CurrentBitmap.Dispose(); 
_CurrentBitmap = _Temp;