clipboard image to image control via code

K

Keith G Hicks

I got some code off the MS website (see below) that will allow me to copy
clipboard info to a variable. It works fine. I need to (via code) paste an
image that's in the clipboard into an image control on a form. I can't seem
to get it to work. I'm probably going about it all wrong and wondered if
anyone can make any suggestions. I know you can set the Picture property to
a bmp or jpg file ( as I've done that many times before) but I need to use
what's on the clipboard. I'm just using an image control and trying to set
its Picture property to the ClipBoard_GetData() function like this:

me.ImageCtrl1.Picture = ClipBoard_GetData()

Hope someone can help.

Thanks,

Keith

=============================
Here's the clipboard code from MS:

Option Compare Database
Option Explicit

Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As
Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal dwBytes 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
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal
lpString2 As Any) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Function ClipBoard_GetData()
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim MyString As String
Dim RetVal As Long

If OpenClipboard(0&) = 0 Then
MsgBox "Cannot open Clipboard. Another app. may have it open"
Exit Function
End If

' Obtain the handle to the global memory
' block that is referencing the text.
hClipMemory = GetClipboardData(CF_TEXT)
If IsNull(hClipMemory) Then
MsgBox "Could not allocate memory"
GoTo OutOfHere
End If

' Lock Clipboard memory so we can reference
' the actual data string.
lpClipMemory = GlobalLock(hClipMemory)

If Not IsNull(lpClipMemory) Then
MyString = Space$(MAXSIZE)
RetVal = lstrcpy(MyString, lpClipMemory)
RetVal = GlobalUnlock(hClipMemory)

' Peel off the null terminating character.
MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
Else
MsgBox "Could not lock memory to copy string from."
End If

OutOfHere:

RetVal = CloseClipboard()
ClipBoard_GetData = MyString

End Function
 
S

Stephen Lebans

Hmmm. there is an Image control to Clipboard solution but I do not see a
Clipboard to Image control sample. The code is there in a couple of my
projects that copies the Clipboard contents to an Image control.

Here's Image control to ClipBoard(you can just reverse the process).
http://www.lebans.com/imagecontroltoclipboard.htm

and there's code in here to copy ClipBoard to a disk file:
http://www.lebans.com/oletodisk.htm

It's a complex issue if you have no experience in this area. The Picture
prop of an Image will only accept a string pointing to an disk based Image
file. But if you want to bypass writing the Clipboard contents to a temp
disk file then you can work directly with the PictureData property. This is
a specially formatted Clipboard Format Metafile or CF_DIB(Device Independant
Bitmap).

If you are using different Image file types then I would suggest you:
Copy Clipboard contents to StdPicture
Convert StdPicture to CF_DIB or CF_Metafile
Copy to PictureData property

All of the code required is in the projects on my site. I just don't
remember exactly which ones. Getting old I guess.
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
K

Keith G Hicks

Thanks for the response :). Much appreciated. I'll check it all out.
-keith

"Stephen Lebans" <[email protected]>
wrote in message Hmmm. there is an Image control to Clipboard solution but I do not see a
Clipboard to Image control sample. The code is there in a couple of my
projects that copies the Clipboard contents to an Image control.

Here's Image control to ClipBoard(you can just reverse the process).
http://www.lebans.com/imagecontroltoclipboard.htm

and there's code in here to copy ClipBoard to a disk file:
http://www.lebans.com/oletodisk.htm

It's a complex issue if you have no experience in this area. The Picture
prop of an Image will only accept a string pointing to an disk based Image
file. But if you want to bypass writing the Clipboard contents to a temp
disk file then you can work directly with the PictureData property. This is
a specially formatted Clipboard Format Metafile or CF_DIB(Device Independant
Bitmap).

If you are using different Image file types then I would suggest you:
Copy Clipboard contents to StdPicture
Convert StdPicture to CF_DIB or CF_Metafile
Copy to PictureData property

All of the code required is in the projects on my site. I just don't
remember exactly which ones. Getting old I guess.
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top