Skew image unto another image

戏子无情 提交于 2019-12-13 07:06:14

问题


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.

So far I have this:

'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.

EDIT I 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:

An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication4.exe

Additional information: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.PaintEventArgs'.


回答1:


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

Here is the result I got:

Using these two files:

and

You can play with parameters to ensure better fit, but it should be good enough to prove the concept.

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.


来源:https://stackoverflow.com/questions/25326418/skew-image-unto-another-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!