2012-04-16 65 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;