Victor Image Processing Library How-to Tips

Images and the Clipboard in Visual Basic

The Victor Library makes it easy to work with images on the clipboard. We can work directly with the clipboard and the images. It'll be quick and easy.

To copy an image to the clipboard, we first convert it into a packed DIB. This is the standard format for Windows clipboard images. Then assign the DIB to the clipboard. After that Windows takes full responsibility for the image and all other image applications can access the DIB.

To paste an image from the clipboard, we first check the clipboard and see if there's a DIB out there. If so, we get the DIB and create a copy of it as a Victor image. When we're done we release the data back to the clipboard so it again becomes available to other applications.

Copy an Image to the Clipboard - the Source Code for VB

Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher.
........... Add these declarations to your Global module ...........
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Function dibtoimage Lib "VIC32.DLL" (ByVal dibaddr As Long, ByRef image As imgdes) As Long
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function imagetodib Lib "VIC32.DLL" (srcimg As imgdes, dib As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Declare Function EmptyClipboard Lib "user32" () As Long
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalHandle Lib "kernel32" (ByVal Addr As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
........... Add these function definitions to a form module, .frm  ...........
Private Sub mnuCopy_Click()
Dim dibaddr As Long
Dim rcode As Long
Dim hMem As Long
Dim dummy As Long
             ' Make sure the clipboard can be opened
   If OpenClipboard(hwnd) Then
       dummy = EmptyClipboard
             ' Take the image in vimage (it's globally defined)
             ' and convert to a packed dib for the clipboard
       rcode = imagetodib(vimage, dibaddr)
       If (rcode = NO_ERROR) Then
           hMem = GlobalHandle(dibaddr)
           dummy = GlobalUnlock(hMem)
             ' Put the packed dib on the clipboard
           dummy = SetClipboardData(vbCFDIB, hMem)
           dummy = CloseClipboard
       End If
       dummy = MsgBox("Could not open clipboard")
   End If
End Sub

' Paste an Image from the Clipboard
Private Sub mnuPaste_Click()
Dim dummy As Long
Dim rcode As Long
Dim tempimage As imgdes
Dim hMem As Long
Dim dibaddr As Long
            ' If there's a DIB on the clipboard, get it
   If Clipboard.GetFormat(vbCFDIB) Then
       rcode = OpenClipboard(hwnd)
       hMem = GetClipboardData(vbCFDIB)
       dibaddr = GlobalLock(hMem)
       If dibaddr Then
           rcode = dibtoimage(dibaddr, tempimage)
       End If
       GlobalUnlock (hMem)
       dummy = CloseClipboard
   End If
   If (rcode = NO_ERROR) Then
            ' Replace current globally defined image
            ' with the new image from the clipboard
       freeimage vimage
       copyimgdes tempimage, vimage
       BackColor = &HFFFFFF  ' Erase previous image
            ' Repaint the screen to display the new image
   End If
End Sub
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code

Copyright © 2000 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.