Vb.net 将图像倾斜到另一个图像

Vb.net 将图像倾斜到另一个图像,vb.net,draw,skew,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 transparen

我正在使用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'


我创建了一个自定义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
以下是我得到的结果:

使用以下两个文件:

您可以使用参数来确保更好的拟合,但它应该足够好来证明这一概念

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


可能重复的请提供一个屏幕截图,说明您试图实现的目标。@neolik这是图像的链接:我无法在帖子中添加图像,因为我没有10的声誉。没有侵犯版权的意图。好了,我们就到了。你发布了两段代码。请指定它们所属的位置,即,第一段代码工作的事件处理程序或重写。它属于一个按钮,合并了我放在TestFolder中的两张图片。第二个块也属于一个按钮,应该会扭曲我想要的图像。这个过程是单击按钮以创建倾斜图像,然后单击按钮将两个图像合并在一起。因此,我创建了一个新表单,复制了您编写的代码,并添加了一个PictureBox。当我尝试构建它时,会出现3个错误:
-错误3'Image'不是'WindowsApplication7.CustomPictureBox'的成员//
-警告2的公共子新()'在设计器生成的类型'WindowsApplication7.CustomPictureBox'中,应调用InitializeComponent方法。
///code>-错误1为类'CustomPictureBox'指定的基类'System.Windows.Forms.PictureBox'不能与其他部分类型之一的基类'System.Windows.Forms.Form'不同。
@user3902533:您应保持表单代码不变。创建一个名为CustomPictureBox的新类,它将位于其自己的单独文件CustomPictureBox.vb中。构建项目。将此新控件放到窗体上。表单此时将没有代码,只有设计器中的代码,但您无论如何都不会编写它。开始这个项目,你应该会在屏幕上看到结果。现在我已经很晚了,我有点困惑(对不起)。从一开始:我创建了一个新项目。我在设计中添加了一个图片盒。我创建了一个名为CustomPictureBox的新类,在其中复制了您提交的代码。然后呢?因为当我构建和调试时,什么都没有happens@user3902533:您创建了一个新项目。不要在designer中添加任何内容。创建一个名为CustomPictureBox的自定义类,并将上面的代码复制到其中。构建项目。您应该会在工具箱中看到一个新控件。这不是一个图片框控件,它是另一个。把它放到表格上。就这样。启动程序(F5)并享受!