Skip to content

BitMap Zooming problem ??

Essam Ali

Asked Jun 18, 2007, 7:21 AM EDT3 answers
Share
LinkedInX


In my program i need to zoom in and zoom out alot
so
after multiple of zooming in and out the image go damage

 

and that is the code if any one have other ideas or programming hint plz help me fast

#region Zooming Methods

        /// <summary>
        /// Make the PictureBox dimensions larger to effect the Zoom.
        /// </summary>
        /// <remarks>Maximum 5 times bigger</remarks>
        
        private void ZoomIn()
        {
            
            if ( ( PicBox.Width < ( MINMAX * OuterPanel.Width ) ) &&
                ( PicBox.Height < ( MINMAX * OuterPanel.Height ) ) )
            {
                
                PicBox.Width = Convert.ToInt32 ( PicBox.Width * ZOOMFACTOR );
                PicBox.Height = Convert.ToInt32 ( PicBox.Height * ZOOMFACTOR );
                NewImage(Class1.bmp,PicBox.Width,PicBox.Height);
                PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                ZOOMINGTIMES++;
            }
        }

        /// <summary>
        /// Make the PictureBox dimensions smaller to effect the Zoom.
        /// </summary>
        /// <remarks>Minimum 5 times smaller</remarks>
        private void ZoomOut()
        {
            if ( ( PicBox.Width > ( OuterPanel.Width / MINMAX ) ) &&
                ( PicBox.Height > ( OuterPanel.Height / MINMAX ) ) )
            {
                PicBox.SizeMode = PictureBoxSizeMode.StretchImage;
                PicBox.Width = Convert.ToInt32 ( PicBox.Width / ZOOMFACTOR );
                PicBox.Height = Convert.ToInt32 ( PicBox.Height / ZOOMFACTOR );
                NewImage(Class1.bmp, PicBox.Width, PicBox.Height);
                ZOOMINGTIMES--;
            }        
        }

        #endregion

        #region Mouse events

        /// <summary>
        /// We use the mousewheel to zoom the picture in or out
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PicBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if ( e.Delta < 0 )
            {
                ZoomOut();
            }
            else
            {
                 ZoomIn();
            }
        }


        private void NewImage(Bitmap bmp1, int newWidth, int newHeight)
        {
            iniImg = Class1.bmp;
            changedImg = new Bitmap(iniImg, newWidth, newHeight);
            PicBox.Image = changedImg;
            Class1.bmp = changedImg;
            g = Graphics.FromImage(changedImg);
        }





and that is the image before zooming in and out

http://www.arabteam2000-forum.com/uploads/monthly_06_2007/post-30356-1182179495.jpg
and that is after
http://www.arabteam2000-forum.com/uploads/monthly_06_2007/post-30356-1182179483.jpg

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    Generally you want to handle zooming using a stack structure.  As you zoom, save the images in the previous zoom in the stack.  When you zoom back out, pop the stack.

    Stack<Image> ImageStack
    0
  • 0
    ty
    i think in a way like that
    but the problem
    i make a changes after zoom draw some objects on the pictuer then zoom it back
    if i do what u say the changes will be deleted

    example
    i zoom 5X draw cirlcles in a certian postions and when i zooming to any other  size  using the the old pictuer all the cirlcles i draw will be deleted
    0
  • Mike Gold
    0
    You should not be zooming by stretching the picture box. Keep the picture box at normal size in non-stretch mode.  Just scale the bitmap and each time you zoom.  Each time you scale, store the bitmap on the stack.  Keep in mind, it's only the bitmap you want to change here. As long as you store the bitmap at each zoom on the stack, you will not degrade the image when you zoom back out.
    0