0

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.

application

When save the preview picture box (BtnSavePreview), the image its correct, but not work for the original image BtnSaveCrop, Im using the next 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

Thanks

  • There's a custom class with all the methods needed to perform this kind of operations here: [Translate Rectangle Position in Zoom Mode Picturebox](https://stackoverflow.com/a/53802043/7444103). C# code, translatable. – Jimi Feb 14 '19 at 20:22

1 Answers1

0

Some fixes working now:

Private Sub BtnSaveCrop_Click(sender As Object, e As EventArgs) Handles BtnSaveCrop.Click

    Dim CropRect As New Rectangle(-PnlPan.AutoScrollPosition.X, -PnlPan.AutoScrollPosition.Y, PnlPan.Width - SystemInformation.VerticalScrollBarWidth, PnlPan.Height - SystemInformation.HorizontalScrollBarHeight)

    Dim Left As Integer = CropRect.Left * (ImgOrig.Width / PicPreview.Width)
    Dim Top As Integer = CropRect.Top * (ImgOrig.Height / PicPreview.Height)
    Dim Right As Integer = CropRect.Right * (ImgOrig.Width / PicPreview.Width)
    Dim Bottom As Integer = CropRect.Bottom * (ImgOrig.Height / PicPreview.Height)


    Dim cropArea As New Rectangle(Left, Top, Right, Bottom)

    Dim img As New Bitmap(Right - Left, Bottom - Top)
    Dim gr As Graphics = Graphics.FromImage(img)
    gr.DrawImage(ImgOrig, New Rectangle(0, 0, cropArea.Width, cropArea.Height), cropArea, GraphicsUnit.Pixel)
    img.Save("C:\_workingFILES\image_crop.jpg", Imaging.ImageFormat.Jpeg)


End Sub

thanks