GUI display of bitmap image from clipboard

Get help with using AutoHotkey and its commands and hotkeys
steveadams
Posts: 4
Joined: 26 Oct 2016, 19:33

GUI display of bitmap image from clipboard

23 Nov 2016, 02:00

Hi All,

I'm getting a bitmap from the clipboard and displaying it in a GUI.
Here is some sample code:

Code: Select all

; Open the ClipBoard
While (! DllCall("OpenClipboard", "UInt", 0))
    Sleep, 10
Critical, On

; Get Bitmap data
If (Handle := DllCall("GetClipboardData", "UInt", 17))
{
    Size := DllCall("GlobalSize", "UInt", Handle)
    Pointer := DllCall("GlobalLock", "UInt", Handle)
    VarSetCapacity(Data, Size)
    DllCall("RtlMoveMemory", "UInt", &Data, "UInt", Pointer, "UInt", Size)
    DllCall("GlobalUnlock", "UInt", Handle)
}

; Close Clipboard
DllCall("CloseClipboard")
Critical, Off

; Quit if no bitmap
If (! Handle)
{
    MsgBox, No bitmap on clipboard.
    ExitApp
}

; Write file.
VarSetCapacity(Header, 14, 0)
NumPut(Ord("B"), Header, 0, "Char")
NumPut(Ord("M"), Header, 1, "Char")
FileDescriptor := FileOpen("clip.bmp", "w")
FileDescriptor.RawWrite(Header, 14)
FileDescriptor.RawWrite(Data, Size)
FileDescriptor.Close()

; Show the picture.
Pic := LoadPicture("clip.bmp")
Gui, Add, Pic, w600 h-1 vPic +Border, % "HBITMAP:*" Pic
Gui, Show
Return

GuiClose:
ExitApp
I'd prefer not to have to write the image to a file.
Is there a way to get an HBITMAP handle directly from the clipboard data?

Thanks,
Steve
tmplinshi
Posts: 1589
Joined: 01 Oct 2013, 14:57

Re: GUI display of bitmap image from clipboard

23 Nov 2016, 03:12

Code: Select all

if DllCall("OpenClipboard", "uint", 0) {
	if DllCall("IsClipboardFormatAvailable", "uint", 8) {
		hBitmap := DllCall("GetClipboardData", "uint", 2)
	}
	DllCall("CloseClipboard")
}

Gui, Add, Pic, vPic +Border, % "HBITMAP:*" hBitmap
Gui, Show
Return

GuiClose:
ExitApp
steveadams
Posts: 4
Joined: 26 Oct 2016, 19:33

Re: GUI display of bitmap image from clipboard

23 Nov 2016, 03:44

Great. Thanks.

An extension to the question. I'd also like to be able to do it from a saved copy of ClipBoardAll.
I am currently doing it by writing a file. With your method, I can put it back on the clipboard.
Is there another way?

Thanks,
Steve
tmplinshi
Posts: 1589
Joined: 01 Oct 2013, 14:57

Re: GUI display of bitmap image from clipboard

23 Nov 2016, 08:48

How about save clipboard image to file, instead of saving the ClipboardAll.

Code: Select all

SaveClipImgToFile("clip.jpg")

SaveClipImgToFile(FileName) {
	pToken := Gdip_Startup()
	pBitmap := Gdip_CreateBitmapFromClipboard()
	Gdip_SaveBitmapToFile(pBitmap, FileName)
	Gdip_DisposeImage(pBitmap)
	Gdip_Shutdown(pToken)
}
Require Gdip.ahk libarary. Search the forum for this file.

Return to “Ask For Help”

Who is online

Users browsing this forum: RyConnMD, tatagi and 31 guests