我有一张预览图片,我可以在其中缩放和拖动图像,我需要保存预览图片框中显示的图像的相同部分,但要裁剪为真实图像(1274x2105 像素)。
保存预览图片框(BtnSavePreview)时,图像正确,但不适用于原始图像 BtnSaveCrop,我使用下一个代码。
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
谢谢