Wednesday, June 27, 2012

Zooming in, zooming out with mouse scrolling and fit to screen


private System.Windows.Forms.PictureBox PictureBox;
 private System.Windows.Forms.Panel OuterPanel;
 private System.Windows.Forms.ToolStripButton FitToWidthButton;

 private double ZOOMFACTOR = 1.25;   
 private int MINMAX = 5;           
 
Add the following event handler

 this.OuterPanel.MouseEnter += new System.EventHandler(PicBox_MouseEnter);
            this.PictureBox.MouseEnter += new EventHandler(PicBox_MouseEnter);
            this.OuterPanel.MouseWheel += new MouseEventHandler(PicBox_MouseWheel);
   this.FitToWidthButton.Click += new System.EventHandler(this.FitToWidthButton_Click);
       

   private void PicBox_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                ZoomIn();
             
            }
            else
            {
                ZoomOut();
             
            }
        }
 private void PicBox_MouseEnter(object sender, EventArgs e)
        {
            if (PictureBox.Focused == false)
            {
                PictureBox.Focus();
            }
        }

     
  Zooming Methods

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

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

  private void FitToWidthButton_Click(object sender, EventArgs e)
      {
          if (FitToWidthButton.Text == "Fit to Screen")
          {
              handleFitToWindow(true);
              FitToWidthButton.Text = "Actual Size";
          }
          else
          {
              handleFitToWindow(false);
              FitToWidthButton.Text = "Fit to Screen";
          }
      }
      private void handleFitToWindow(bool doFit)
      {
          try
          {
              /* Don’t perform any operation if no image is loaded. */
              if (this.PictureBox.Tag != null)
              {
                  if (doFit)  /* We’re fitting it to the window, and centering it. */
                  {
                      /* Create a temporary Image.

                      * Always work from the original image, stored in the Tag.
                      */

                      Image tempImage = (Image)this.PictureBox.Tag;

                      /* Calculate the dimensions necessary for an image to fit. */
                      Size fitImageSize = this.getScaledImageDimensions(
                      tempImage.Width, tempImage.Height, this.PictureBox.Width, this.PictureBox.Height);

                      /* Create a new Bitmap from the original image with the new dimensions.

                      * The constructor for the Bitmap object automatically scales as necessary.
                      */

                      Bitmap imgOutput = new Bitmap(tempImage, fitImageSize.Width, fitImageSize.Height);

                      /* Clear any existing image in the PictureBox. */
                      this.PictureBox.Image = null;

                      /* When fitting the image to the window, we want to keep it centered. */

                      this.PictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
                    
                      /* Finally, set the Image property to point to the new, resized image. */
                      this.PictureBox.Image = imgOutput;
                 
                  }

                  else  /* Restore the image to its original size */
                  {
                      /* Clear any existing image int he PictureBox. */
                      this.PictureBox.Image = null;

                      /* Set the resize more to Normal; this will place the image

                      * in the upper left-hand corner, clipping the image as required.
                      */
                      Image tempImage = (Image)this.PictureBox.Tag;
                 
                      this.PictureBox.SizeMode = PictureBoxSizeMode.Normal;
                  
                      /* Finally, set the Image property to point to the original image. */

                      this.PictureBox.Image = (Image)this.PictureBox.Tag;
                   
                  }
              }
          }
          catch (System.Exception e)
          {
              Console.WriteLine(e);
          }
      }

 

No comments:

Post a Comment