title: VB.net calculate thumbnail coordinates to save crop from original image
body: <p>I have a preview picture where I can zoom and drag an image, what I need is to save the same portion of image shown in the preview picturebox but to real image (1274x2105 px) cropped.</p>
  
  <p><a href="https://i.stack.imgur.com/Ik04Q.jpg" rel="nofollow noreferrer">application</a></p>
  
  <p>When save the preview picture box (BtnSavePreview), the image its correct, but not work for the original image BtnSaveCrop, Im using the next code.</p>
  
  <pre><code>Private Sub BtnSavePreview_Click(sender As Object, e As EventArgs) Handles BtnSavePreview.Click
  
      Dim img As New Bitmap(PnlPan.Width - SystemInformation.VerticalScrollBarWidth, PnlPan.Height - SystemInformation.HorizontalScrollBarHeight)
      Dim gr As Graphics = Graphics.FromImage(img)
      gr.DrawImage(PicPreview.Image, New Rectangle(Point.Empty, img.Size), New Rectangle(-PnlPan.AutoScrollPosition.X, -PnlPan.AutoScrollPosition.Y, img.Width, img.Height), GraphicsUnit.Pixel)
      img.Save("C:\_workingFILES\image_preview_crop.jpg", Imaging.ImageFormat.Jpeg)
  
  End Sub
  
  Private Sub BtnSaveCrop_Click(sender As Object, e As EventArgs) Handles BtnSaveCrop.Click
      Dim CropRect As New Rectangle(-PnlPan.AutoScrollPosition.X, -PnlPan.AutoScrollPosition.Y, ImgZoomPan.Width, ImgZoomPan.Height)
  
  
      Dim Left As Integer = CropRect.Left * (ImgOrig.Width / ImgZoomPan.Width)
      Dim Top As Integer = CropRect.Top * (ImgOrig.Height / ImgZoomPan.Height)
      Dim Right As Integer = CropRect.Right * (ImgOrig.Width / ImgZoomPan.Width)
      Dim Bottom As Integer = CropRect.Bottom * (ImgOrig.Height / ImgZoomPan.Height)
  
      Dim img As New Bitmap(Right - Left, Bottom - Top)
      Dim gr As Graphics = Graphics.FromImage(img)
      gr.DrawImage(ImgOrig, New Rectangle(0, 0, Right - Left, Bottom - Top), New Rectangle(Right, Bottom, img.Width, img.Height), GraphicsUnit.Pixel)
      img.Save("C:\_workingFILES\image_crop.jpg", Imaging.ImageFormat.Jpeg)
  
  End Sub
  </code></pre>
  
  <p>Thanks</p>