Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / Visual Basic

Copying graphics with BitBlt (.NET Style)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (15 votes)
29 Dec 2003 197K   2.5K   26  
When Graphics.Clone() just won't do.
Imports System.Drawing.Drawing2D

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents src As System.Windows.Forms.PictureBox
    Friend WithEvents dest As System.Windows.Forms.PictureBox
    Friend WithEvents srcDraw As System.Windows.Forms.Button
    Friend WithEvents dstCopy As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.src = New System.Windows.Forms.PictureBox()
        Me.dest = New System.Windows.Forms.PictureBox()
        Me.srcDraw = New System.Windows.Forms.Button()
        Me.dstCopy = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'src
        '
        Me.src.BackColor = System.Drawing.Color.Black
        Me.src.Location = New System.Drawing.Point(16, 16)
        Me.src.Name = "src"
        Me.src.Size = New System.Drawing.Size(200, 256)
        Me.src.TabIndex = 0
        Me.src.TabStop = False
        '
        'dest
        '
        Me.dest.BackColor = System.Drawing.Color.Black
        Me.dest.Location = New System.Drawing.Point(232, 16)
        Me.dest.Name = "dest"
        Me.dest.Size = New System.Drawing.Size(208, 256)
        Me.dest.TabIndex = 1
        Me.dest.TabStop = False
        '
        'srcDraw
        '
        Me.srcDraw.Location = New System.Drawing.Point(16, 280)
        Me.srcDraw.Name = "srcDraw"
        Me.srcDraw.Size = New System.Drawing.Size(200, 23)
        Me.srcDraw.TabIndex = 2
        Me.srcDraw.Text = "Source Draw"
        '
        'dstCopy
        '
        Me.dstCopy.Location = New System.Drawing.Point(232, 280)
        Me.dstCopy.Name = "dstCopy"
        Me.dstCopy.Size = New System.Drawing.Size(208, 24)
        Me.dstCopy.TabIndex = 3
        Me.dstCopy.Text = "BitBlt Copy"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 406)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dstCopy, Me.srcDraw, Me.dest, Me.src})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Declare Auto Function BitBlt Lib "GDI32.DLL" ( _
        ByVal hdcDest As IntPtr, _
        ByVal nXDest As Integer, _
        ByVal nYDest As Integer, _
        ByVal nWidth As Integer, _
        ByVal nHeight As Integer, _
        ByVal hdcSrc As IntPtr, _
        ByVal nXSrc As Integer, _
        ByVal nYSrc As Integer, _
        ByVal dwRop As Int32) As Boolean


    Private Sub srcDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles srcDraw.Click
        Dim g As Graphics = src.CreateGraphics
        Dim r As RectangleF = New RectangleF(0, 0, src.Width, src.Height)
        Dim b As LinearGradientBrush = New LinearGradientBrush(r, Color.Blue, Color.DarkBlue, LinearGradientMode.Vertical)
        g.FillRectangle(b, r)
    End Sub

    Private Function copyRect(ByVal src As PictureBox, ByVal rect As RectangleF) As Bitmap
        Dim srcPic As Graphics = src.CreateGraphics             'Get a Graphics Object from the form 
        Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic) 'Create a EMPTY bitmap from that graphics 
        Dim srcMem As Graphics = Graphics.FromImage(srcBmp)     'Create a Graphics object in memory from that bitmap 
        Dim HDC1 As IntPtr = srcPic.GetHdc                      'get the IntPtr's of the graphics 
        Dim HDC2 As IntPtr = srcMem.GetHdc                      'get the IntPtr's of the graphics 

        'get the picture 
        BitBlt(HDC2, 0, 0, rect.Width, rect.Height, HDC1, rect.X, rect.Y, 13369376)

        'Clone the bitmap so we can dispose this one 
        copyRect = srcBmp.Clone()

        'Clean Up 
        srcPic.ReleaseHdc(HDC1)
        srcMem.ReleaseHdc(HDC2)
        srcPic.Dispose()
        srcMem.Dispose()
        srcMem.Dispose()
    End Function

    Private Sub dstCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dstCopy.Click
        dest.Image = CType(copyRect(src, New RectangleF(0, 0, 50, src.Height)), Bitmap).Clone
    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.



Comments and Discussions