GUI display of bitmap image from clipboard

Get help with using AutoHotkey (v1.1 and older) 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: 1604
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", "ptr", 0) {
	if DllCall("IsClipboardFormatAvailable", "uint", 2) {
		hBitmap := DllCall("GetClipboardData", "uint", 2, "ptr")
	}
	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: 1604
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.
Vikas_Asodariya
Posts: 1
Joined: 08 Apr 2021, 23:52

Re: GUI display of bitmap image from clipboard

21 Jun 2021, 05:38

Thank you so much @steveadams !!
I was desperately looking for this script. I am using your script with "WinSnipping tool(ahk)". Which can magnify any selected part of window very smoothly.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 235 guests