0

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

但是每次我运行它,我都会收到这个错误:

WindowsApplication4.exe 中发生了“System.InvalidCastException”类型的未处理异常

附加信息:无法将“System.Windows.Forms.MouseEventArgs”类型的对象转换为“System.Windows.Forms.PaintEventArgs”类型。

4

1 回答 1

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

这是我得到的结果:

在此处输入图像描述

使用这两个文件:

在此处输入图像描述在此处输入图像描述

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

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

于 2014-08-17T14:10:55.653 回答