问题描述
我正在使用Visual Studio Express2012。
我有一个主背景图像,我想倾斜一个图像并将其添加到主背景中。
I'm using Visual Studio Express 2012.I have a main background image, and I want to skew an image and add it to the main background.
到目前为止,我有:
'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()
我设法将图像绘制在另一个图像上,但是我无法使其倾斜并放置在正确的位置使用像素位置。
I managed to draw the image on top of another one, but I'm not able to skew it and put in the right place using pixel location.
编辑
我还添加了以下代码:
EDITI also added this code:
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)
但是每次运行它时,都会出现此错误:
But everytime I run it, I get this error:
其他信息:无法转换类型为
的对象'System.Windows.Forms.MouseEventArgs'键入
'System.Windows.Forms.PaintEventArgs'。
Additional information: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.PaintEventArgs'.
推荐答案
I创建了一个自定义PictureBox之后,您可以进一步对其进行扩展以添加指定背景和覆盖图像的属性,以及动态计算缩放系数。我使用MsPaint从您的问题中剪切了图像并将其保存为两个单独的文件。要使用该控件,请将其放在窗体上,并确保图像文件位于指定位置。
I created a custom PictureBox, you can further expand it to add properties specifying background and overlay images, as well as dynamically calculate scaling factors. I cut images from your question using MsPaint and saved them as two separate files. To use, drop this new control on the form, and make sure image files exist at the specified locations.
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
这是我得到的结果:
使用这两个文件:
和
您可以使用参数来确保更合适,但应该足以证明这一概念。
You can play with parameters to ensure better fit, but it should be good enough to prove the concept.
注意:为什么您可能会遇到问题,是因为您需要3个而不是4个终点形成本文中指定的平行四边形:
Note: Why you may have got problems with it, is because you need 3, not 4 destination points to form a parallelogram, as specified in this article:
- 。
- How to: Rotate, Reflect, and Skew Images @ MSDN.
这篇关于将图像倾斜到另一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!