如何获得一张拉伸的图像?

时间:2015-02-14 13:38:32

标签: c# .net vb.net

我有一个图片框,里面有拉伸的图像。我在图像的一部分上有一个选择矩形。我想将选择保存为新图像。它没有考虑主图片框的拉伸,因此它没有捕获正确的选择矩形坐标。这是我的代码片段(当调用它时,已经设置了选择矩形):

Dim sourcebmp As New Bitmap(picScan.Image)
Dim destinationbmp As New Bitmap(selection.Width, selection.Height)
Dim gr As Graphics = Graphics.FromImage(destinationbmp)
Dim destinationrectangle As New Rectangle(0, 0, selection.Width, selection.Height)
gr.DrawImage(sourcebmp, destinationrectangle, selection, GraphicsUnit.Pixel)
picScan.Image = New Bitmap(destinationbmp)

1 个答案:

答案 0 :(得分:1)

我找到了一个解决问题的示例:

'Take a temporary snapshot of the picture box:
Dim tempBitmap As Bitmap = New Bitmap(picScan.ClientSize.Width, picScan.ClientSize.Height)
picScan.DrawToBitmap(tempBitmap, picScan.ClientRectangle)

'Copy the selection rectangle from the temporary snapshot to the target bitmap:
Dim target As New Bitmap(selection.Width, selection.Height)
Using g As Graphics = Graphics.FromImage(target)
   g.DrawImage(tempBitmap, target.GetBounds(0), selection, GraphicsUnit.Pixel)
End Using

tempBitmap.Dispose()
picScan.Image = target