Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
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;
       }

     }


When ResetBitmap() is called after rotation then it shows "Parameter is not valid". but if

this.imageHandler.CurrentBitmap.Dispose(); is commented then works fine.

But if Flip() method is called several times then "Out of memory" exception is shown.

How can I solve it?
Posted
Updated 16-Apr-12 15:39pm
v2

The problem is with the way you are handling before and after modification of the bitmap. In RestorePrevious() function, you are assigning current bitmap (_currentBitmap) to a new variable (_bitmapbeforeProcessing). That will not create a separate copy as you may think, instead all that is happening is both variables are pointing to the same resource in memory. So, when you call dispose in Flip() function, the bitmap referenced by _bitmapbeforeProcessing will also be disposed. Hence you will get the error in ResetBitmap().

And regarding out of memory issue, well, that would be obvious as you will be creating new objects in heap and eventually it will run out.
 
Share this answer
 
As I pointed out earlier, your backup copy and the active copy of the bitmap as pointing to the same resource in memory and thus when one is disposed, other will also be disposed. So, you can start by making changes to RestorePrevious() so that the backup copy contains a fresh copy. You can try something like
C#
internal void RestorePrevious()
{
    _bitmapbeforeProcessing = _currentBitmap.Clone();
}


See if that helps.

However, some of the logic doesn't make sense to me. Like why create a dummy bitmap object in CurrentBitmap property? It will only cause logical errors later on which are harder to debug should a problem occur. Also the way you are handling backup-restore procedure could be made simpler. Your code contains all the right stuff, but can be made better by a simple re-design/structuring of the code. Always refactor your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900