title: vb.net how can zoom image
body: <p>I have a Custom Control for viewing images with zooming facility but without scrollBar, and its working condition is good. Actually The Custom Control is a <strong>Panel with a Picutrbox on it</strong>. I also using a TrackBar for zooming in/out the image. It is also working better.</p>
  
  <p>But I am not fully satisfied, even it is covering the purpose of my App because I need <strong>zooming PictureBox based on center point</strong>. Now it is anchoring Top Left.</p>
  
  <p>Another One is when Zooming out the image, the image goes to zero size at TrackBar's Zero. Even I limited zoom level to the panel size and working good, my unsatisfaction taking place here  also, as it is not responding at zero level of TrackBar. Here I need, the <strong>original size of image</strong> loaded in picture box have to go for <strong>100% of TrackBar</strong> and when image reached at <strong>Custom controle size</strong> have to go for <strong>0% of TrackBar</strong>. Then I will fullfilled.</p>
  
  <p>I figuring my code here.........</p>
  
  <p><strong>My Custom Control is a User Control Inheriting from Panel.</strong></p>
  
  <p><strong><em>code for cutom control :</em></strong></p>
  
  <pre><code>Public Class ImageViewer
  Inherits Panel
  
  Dim AutoScaleDimensions As SizeF
  Dim AutoScaleMode As AutoScaleMode
  
  Protected Overrides Sub DefWndProc(ByRef m As Message)
      If m.Msg &lt;&gt; 131 Then
          MyBase.DefWndProc(m)
      End If
  End Sub
  
  End Class
  </code></pre>
  
  <p><strong>On Form1</strong>, I Placed my Custom Control- <strong>ImageViewer1</strong> and also placed <strong>PicutreBox1 <em>with in</em> ImageViewer1</strong>, Palced <strong>Button1 and TrackBar1 on Form1</strong></p>
  
  <p><strong>Changed Properties as follows</strong><br>
  ImageViewer1 - AutoScroll=True<br>
  PicutreBox1 - SizeMode=Zoom<br>
  TrackBar1- Maximum=100<br></p>
  
  <p><strong>My Declared Variables are</strong></p>
  
  <pre><code>Dim imgName As String
  Private SliderCenter As Integer = 50
  Private originalImg As Bitmap
  </code></pre>
  
  <p><strong><em>Code for</em>    Button1.Click</strong></p>
  
  <pre><code>    Try
          Dim inputImg As FileDialog = New OpenFileDialog()
  
          inputImg.Filter = "Image File (*.Jpg;*.Bmp;*.Png;*.Gif;*.Tiff;*.Tif;*.PDF)|*.Jpg;*.Bmp;*.Png;*.Gif;*.Tiff;*.Tif;*.PDF"
  
          If inputImg.ShowDialog() = DialogResult.OK Then
  
              imgName = inputImg.FileName
              originalImg = New Bitmap(inputImg.FileName)
              Dim newImg As New Bitmap(imgName)
              PictureBox1.Image = DirectCast(newImg, Image)
  
          End If
          inputImg = Nothing
      Catch ae As System.ArgumentException
          imgName = ""
  
          MessageBox.Show(ae.Message.ToString)
      Catch ex As Exception
          MessageBox.Show(ex.Message.ToString)
      End Try
  </code></pre>
  
  <p><strong><em>Code for</em>    TrackBar1.ValueChanged</strong></p>
  
  <pre><code>If originalImg IsNot Nothing Then
          If TrackBar1.Value &gt; 0 Then
              Dim scale As Double = TrackBar1.Value
              Dim height As Integer = Convert.ToInt32((scale / SliderCenter) * originalImg.Height)
              Dim width As Integer = Convert.ToInt32((scale / SliderCenter) * originalImg.Width)
              PictureBox1.Size = New Size(width, height)
              If PictureBox1.Width &lt;= ImageViewer1.Width Then
                  PictureBox1.Size = New Size(PictureBox1.Width + (ImageViewer1.Width - PictureBox1.Width), ImageViewer1.Height)
              End If
              If PictureBox1.Height &lt;= ImageViewer1.Height Then
                  PictureBox1.Size = New Size(ImageViewer1.Width, PictureBox1.Height + (ImageViewer1.Height - PictureBox1.Height))
              End If
          End If
      End If
  </code></pre>
  
  <p>Please help me in your kind ........ Thank You.</p>