将图像倾斜到另一张图像

时间:2014-08-15 12:29:17

标签: vb.net draw skew

我正在使用Visual Studio Express 2012。 我有一个主背景图像,我想扭曲图像并将其添加到主背景中。

到目前为止,我有这个:

'Create a new bitmap image of the transparent image

Dim overlay As New Bitmap("C:\TestFolder\rectangle.png")

'Create a new bitmap image of the image to you want the transparent image drawn onto

Dim pic As New Bitmap("C:\TestFolder\MyImage.png")

'Create a graphics object from the image to be drawn onto

Dim grx As Graphics = Graphics.FromImage(pic)

'Draw the transparent image into the picture

grx.DrawImage(overlay, 100, 100)

'Dispose the graphics object

grx.Dispose()

'Save the new image that you just put the transparent image on

pic.Save("C:\TestFolder\NewImage.jpg", Imaging.ImageFormat.Jpeg)

'Dispose both new bitmap images because they are not needed anymore

overlay.Dispose()
pic.Dispose()

我设法将图像绘制在另一个图像的顶部,但是我无法将图像倾斜并使用像素位置放在正确的位置。

修改 我还添加了这段代码:

Dim destinationPoints As Point() = { _
    New Point(518, 0), _
    New Point(743, 0), _
    New Point(518, 288), _
    New Point(743, 377)}

Dim image As New Bitmap("C:\TestFolder\this.png")

' Draw the image unaltered with its upper-left corner at (0, 0)

e.Graphics.DrawImage(image, 518, 0)

' Draw the image mapped to the parallelogram

e.Graphics.DrawImage(image, destinationPoints)

但每次我运行它时,都会收到此错误:

  

未处理的类型' System.InvalidCastException'发生了   在WindowsApplication4.exe中

     

附加信息:无法投射类型的对象   ' System.Windows.Forms.MouseEventArgs'输入   ' System.Windows.Forms.PaintEventArgs'

1 个答案:

答案 0 :(得分:0)

我创建了一个自定义PictureBox,您可以进一步扩展它以添加指定背景和叠加图像的属性,以及动态计算缩放因子。我使用MsPaint从您的问题中剪切图像并将它们保存为两个单独的文件。要使用,请在表单上删除此新控件,并确保图像文件存在于指定位置。

Public Class CustomPictureBox : Inherits PictureBox

  Sub New()
    Me.Image = New Bitmap("C:\Admin\image_background.png")
  End Sub

  Protected Overrides Sub OnPaint(pe As PaintEventArgs)
    MyBase.OnPaint(pe)

    Dim image As New Bitmap("C:\Admin\image_overlay.png")

    Dim szScale As Size = image.Size
    szScale.Width /= 4.5
    szScale.Height /= 2

    Dim ptLocation As Point = New Point(98, -17)

    Dim destinationPoints As Point() = {
      ptLocation,
      ptLocation + New Point(szScale.Width, 20),
      ptLocation + New Point(0, szScale.Height)
    }

    pe.Graphics.DrawImage(image, destinationPoints)
  End Sub

End Class

以下是我得到的结果:

enter image description here

使用这两个文件:

enter image description hereenter image description here

你可以使用参数来确保更好的适应性,但它​​应该足以证明这个概念。

注意:为什么您可能遇到问题,因为您需要3个而不是4个目标点来形成平行四边形,如本文所述: