Shove an Image onto the Clipboard with gdip library

A friend reached out to me wondering if you could use AutoHotkey to automate copying an image from a file to your clipboard.  I figured you could with either the WIA COM Object or the gdip library.   While I didn’t get the WIA solution working, I did (by borrowing from Sean) put together a solution with GDIP.  Here’s a link to the Notify function

Here’s the video walking-through my function to Shove an Image onto the Clipboard

Here’s the code to Shove an Image onto the Clipboard

#SingleInstance,Force
#Include B:\Progs\AutoHotkey_L\Lib\Gdip_All.ahk

Browser_Forward::Reload
Browser_Back::

File	:= "B:\Joe + Jon JPEGS\MothersDay.jpg"
Image_to_Clipboard(File,0)
return


Image_to_Clipboard(File_Path,Display_Notification:=0){
  pToken	:= Gdip_Startup()
  pBitmap	:= Gdip_CreateBitmapFromFile(File_Path)
  hBitmap	:= Gdip_CreateHBITMAPFromBitmap(pBitmap)
  Gdip_DisposeImage(pBitmap)
  Gdip_Shutdown(pToken)
  SetClipboardData(8,hBitmap)
  DllCall("DeleteObject", "Uint", hBitmap)
  If (Display_Notification=1)
    Notify("Image on Clipboard",%clipboard%,5,"TS=14 TM=12 GC_=Yellow SI_=500"  ) ;SI=speed In- smaller is faster
  If (Display_Notification=2)
    Notify("Image on Clipboard",%clipboard%,5,"TS=14 TM=12 GC_=Yellow SI_=500  WP=" File_Path ) ;SI=speed In- smaller is faster
}
Return

;~ https://autohotkey.com/board/topic/23162-how-to-copy-a-file-to-the-clipboard/page-2#entry151138
SetClipboardData(nFormat, hBitmap){
  DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)
  hDBI :=	DllCall("GlobalAlloc", "Uint", 2, "Uint", 40+NumGet(oi,44))
  pDBI :=	DllCall("GlobalLock", "Uint", hDBI)
  DllCall("RtlMoveMemory", "Uint", pDBI, "Uint", &oi+24, "Uint", 40)
  DllCall("RtlMoveMemory", "Uint", pDBI+40, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44))
  DllCall("GlobalUnlock", "Uint", hDBI)
  DllCall("OpenClipboard", "Uint", 0)
  DllCall("EmptyClipboard")
  DllCall("SetClipboardData", "Uint", nFormat, "Uint", hDBI)
  DllCall("CloseClipboard")
}

 

 

Comments are closed.