tmighty
tmighty

Reputation: 11389

VB.NET DrawImage to certain position

I tried to rebuild the BitBlt function in VB.NET, and it works not sooo bad, but my image is always blitted/drawn on 0,0 on the destination bitmap.

Does anybody see my mistake?

As one can see, I am trying to copy the rect (0, 0, 50, 50) from the source bitmap to the point (25,25) in the destination bitmap, but it does not do that:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Make a Bitmap to hold the result.
    Dim bm As New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)

    CopyBitmap(Me.PictureBox1.Image, bm, 25, 25, 50, 50, 0, 0)

    Me.PictureBox2.Image = bm

End Sub

Public Sub CopyBitmap(ByRef uSource As Bitmap, ByRef uTarget As Bitmap, ByVal uDestX As Integer, ByVal uDestY As Integer, ByVal uSrcWidth As Integer, ByVal uSrcHeight As Integer, ByVal uSrcX As Integer, ByVal uSrcY As Integer)

    Dim nSrc As New Rectangle
    nSrc = Rectangle.FromLTRB(uSrcX, uSrcY, uSrcX + uSrcWidth, uSrcY + uSrcHeight)

    Dim nDst As New Rectangle
    nDst = Rectangle.FromLTRB(uDestX, uDestY, uDestX + uSrcWidth, uDestY + uSrcHeight)

    Using g As Graphics = Graphics.FromImage(uTarget)
        ' Draw the specified section of the source bitmap to the new one
        g.DrawImage(uSource, nSrc, nDst, GraphicsUnit.Pixel)
    End Using

End Sub

Upvotes: 1

Views: 1911

Answers (1)

tmighty
tmighty

Reputation: 11389

Doh, I swapped nSrc and nDst. The 2nd argument in DrawImage should be nDst, and the 3rd argument should be nSrc.

Upvotes: 1

Related Questions