【问题标题】:How to get a piece of a stretched image?如何获得一张拉伸的图像?
【发布时间】:2015-02-14 21:51:08
【问题描述】:

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

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)

【问题讨论】:

    标签: c# .net vb.net


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2010-09-07
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多