我有一个预览图片,可以缩放和拖动图像,我需要保存预览图片框中显示的图像的相同部分,但裁剪为真实图像(1274x2105 px)。
保存预览图片框(BtnSavePreview)时,图像是正确的,但不适用于原始图像BtnSaveCrop,Im使用下一个代码。
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
谢谢
答案 0 :(得分:0)
一些修复程序现在可以工作:
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
谢谢