Copy the entire contents of a PictureBox to the clipboard

VB does not allow you to copy the full picture of a Form, UserControl or PictureBox to the clipboard. If you use Clipboard.SetData, it only copies a bitmap loaded into these objects. You can get around this limitation and ensure the entire contents are copied, including any graphics you have drawn, by using API methods.

Start a new project in VB. Add a Command button and a PictureBox to the project's form. Make sure the Autoredraw property of the PictureBox is set to 1.

Then add a module and copy the following code into it.

' General functions: 
Private Type RECT 
    Left As Long 
    Top As Long 
    Right As Long 
    Bottom As Long 
End Type 

' GDI functions: 
Private Declare Function BitBlt Lib "gdi32" _
   (ByVal hDestDC As Long, _
    ByVal x As Long, ByVal y As Long, _
    ByVal nWidth As Long, ByVal nHeight As Long, _
    ByVal hSrcDC As Long, _
    ByVal xSrc As Long, ByVal ySrc As Long, _
    ByVal dwRop As Long) As Long 
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source 
' Creates a memory DC 
Declare Function CreateCompatibleDC Lib "gdi32" _
   (ByVal hDC As Long) As Long 
' Creates a bitmap in memory: 
Declare Function CreateCompatibleBitmap Lib "gdi32" _
   (ByVal hDC As Long, _
   ByVal nWidth As Long, ByVal nHeight As Long) As Long 
' Places a GDI Object into DC, returning the previous one: 
Declare Function SelectObject Lib "gdi32" _
   (ByVal hDC As Long, ByVal hObject As Long) As Long 
' Deletes a GDI Object: 
Declare Function DeleteObject Lib "gdi32" _
   (ByVal hObject As Long) As Long 

' Clipboard functions: 
Private Declare Function OpenClipboard Lib "USER32" _
   (ByVal hWnd As Long) As Long 
Private Declare Function CloseClipboard Lib "USER32" () As Long 
Private Declare Function SetClipboardData Lib "USER32" _
   (ByVal wFormat As Long, ByVal hMem As Long) As Long 
Private Declare Function EmptyClipboard Lib "USER32" () As Long 
Private Const CF_BITMAP = 2 

Public Function CopyEntirePicture(ByRef objFrom As Object) As Boolean 
Dim lhDC As Long 
Dim lhBMP As Long 
Dim lhBMPOld As Long 
Dim lWidthPixels As Long
Dim lHeightPixels As Long

    ' Create a DC compatible with the object we're copying 
    ' from: 
    lhDC = CreateCompatibleDC(objFrom.hDC) 
    If (lhDC <> 0) Then 
        ' Create a bitmap compatible with the object we're 
        ' copying from: 
        lWidthPixels = objFrom.ScaleX( _
                           objFrom.ScaleWidth, _
                           objFrom.ScaleMode, _
        lHeightPixels = objFrom.ScaleY( _
                           objFrom.ScaleHeight, _
                           objFrom.ScaleMode, _
        lhBMP = CreateCompatibleBitmap(objFrom.hDC, _
                   lWidthPixels, lHeightPixels) 
        If (lhBMP <> 0) Then 
            ' Select the bitmap into the DC we have created, 
            ' and store the old bitmap that was there: 
            lhBMPOld = SelectObject(lhDC, lhBMP) 
            ' Copy the contents of objFrom to the bitmap: 
            BitBlt lhDC, 0, 0, lWidthPixels, lHeightPixels, _
                objFrom.hDC, 0, 0, SRCCOPY 
            ' Remove the bitmap from the DC: 
            SelectObject lhDC, lhBMPOld 
            ' Now set the clipboard to the bitmap: 
            OpenClipboard 0 
            SetClipboardData CF_BITMAP, lhBMP 

            ' We don't delete the Bitmap here - it is now owned 
            ' by the clipboard and Windows will delete it for us 
            ' when the clipboard changes or the program exits. 
        End If 
        ' Clear up the device context we created: 
        DeleteObject lhDC 
    End If 
End Function 

To try out the method, add this code to the form:

Private Sub Command1_Click() 
    CopyEntirePicture Picture1 
End Sub 

Private Sub Form_Load() 
    Dim iAs Long 
    ' Draw something in the Picture box: 
    With Picture1.Font 
        .Name = "Arial" 
        .Bold = True 
        .Size = 24 
    End With 
    For i = 1 To 20 
        Picture1.ForeColor = QBColor(i Mod 15) 
        Picture1.Print "vbAccelerator" 
    Next i 
End Sub 

When the form loads, the picture box will have some text added to it. When you click the Command button, the entire picture box contents will be copied to the Clipboard, from where you can paste it into Paint, Word etc.