title: Zooming image in C#
body: <p>I have a problem in zooming an image. I have successfully zoomed the image but the image breaks when it is scrolled up or down. </p>
  
  <pre><code>public partial class ImageDetail : UserControl
  {
      public delegate void onClose();
      public event onClose close;
      Image pbImage;
      public ImageDetail(Image img)
      {            
          InitializeComponent();
          pictureBox.Image = img;
          pbImage = img;
          pictureBox.Width = this.pbImage.Width;
          pictureBox.Height = this.pbImage.Height;
          panel2.AutoScroll = true;
          panel2.HorizontalScroll.Maximum = img.Width;
          panel2.VerticalScroll.Maximum   = img.Height;
          panel2.HorizontalScroll.Minimum = 0;
          panel2.VerticalScroll.Minimum   = 0;
          panel2.SetAutoScrollMargin(10, 10);            
      }
  
      private void label2_Click(object sender, EventArgs e)
      {
          if (close != null)
              close(); 
      }        
  
      private void DrawImage(int startX, int startY)
      {            
          if (this.pbImage == null) { return; }
  
          Graphics pbGraphics = this.pictureBox.CreateGraphics();
          BufferedGraphicsContext currentGraphicsContext = BufferedGraphicsManager.Current;
          Rectangle targetRect = new Rectangle(startX, startY, (this.pbImage.Width + tmpWidth), (this.pbImage.Height + tmpHeight));
          using (BufferedGraphics pbGDIBuffer = currentGraphicsContext.Allocate(pbGraphics, targetRect))
          {
              Rectangle drawRect = new Rectangle(startX, startY, (this.pbImage.Width + tmpWidth), (this.pbImage.Height + tmpHeight));
  
              pbGDIBuffer.Graphics.DrawImageUnscaledAndClipped(this.pbImage, drawRect);
              pbGDIBuffer.Render();                
          }
          pictureBox.Width = this.pbImage.Width + tmpWidth;
          pictureBox.Height = this.pbImage.Height + tmpHeight;
      }             
  
      int tmpWidth = 0, tmpHeight = 0;
      private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
      {
          tmpWidth = tmpWidth + ((this.pbImage.Width * 20) / 100);
          tmpHeight = tmpHeight + ((this.pbImage.Height * 20) / 100);
  
          pictureBox.Width = this.pbImage.Width + tmpWidth;
          pictureBox.Height = this.pbImage.Height + tmpHeight;
          pictureBox.Refresh();
          DrawImage(0, 0);
      }
  
      private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e)
      {
          if(tmpWidth &gt; 0)
              tmpWidth = tmpWidth - ((this.pbImage.Width * 20) / 100);
          if(tmpHeight &gt; 0)
              tmpHeight = tmpHeight - ((this.pbImage.Height * 20) / 100);
          if (tmpHeight &lt; 0)
              tmpHeight = 0;
          if (tmpWidth &lt; 0)
              tmpWidth = 0;
          pictureBox.Refresh();
          DrawImage(0, 0);                      
      }
  
      private void panel2_MouseDown(object sender, MouseEventArgs e)
      {
          pictureBox.Width = this.pbImage.Width + tmpWidth;
          pictureBox.Height = this.pbImage.Height + tmpHeight;
      }
  
      private void panel2_MouseUp(object sender, MouseEventArgs e)
      {
          pictureBox.Width = this.pbImage.Width + tmpWidth;
          pictureBox.Height = this.pbImage.Height + tmpHeight;
      }
  
  }
  </code></pre>