Microsoft Visual Basic/GDI+ Operations: Copying a Picture


Copying a picture is the process of getting each pixel of a picture from one document, the source, and reproducing it on another document in the exact same location (x and y coordinates), the target:

Copying a Picture

This operator is easy. From the source, get the (x, y) coordinate of a pixel and assign it to the corresponding (x, y) coordinate on the target document. This can be done as follows:

Copying a Picture

Public Class Exercise

    Private Sub btnCopy_Click(ByVal sender As System.Object,
                              ByVal e As System.EventArgs) Handles btnCopy.Click
        Dim clr As Color
        Dim graph As Graphics
        Dim bmpSource As Bitmap
        Dim bmpDestination As Bitmap
        Dim x As Integer, y As Integer
        Dim intHeight As Integer, intWidth As Integer

        graph = pbxSource.CreateGraphics()
        bmpSource = pbxSource.Image ' CType(pbxSource.Image, Bitmap)
        bmpDestination = New Bitmap(pbxDestination.ClientSize.Width,

        intWidth = bmpSource.Width
        intHeight = bmpSource.Height

        For x = 0 To intWidth - 1
            For y = 0 To intHeight - 1
                clr = bmpSource.GetPixel(x, y)
                bmpDestination.SetPixel(x, y, clr)

        pbxDestination.Image = bmpDestination
    End Sub
End Class

This would produce:

Copying a Picture




Home Copyright © 2010-2012 FunctionX