Paste a saved image into a document

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gyro2222
Posts: 15
Joined: 06 Sep 2017, 11:12

Paste a saved image into a document

Post by gyro2222 » 22 Apr 2020, 13:36

i've been trying to load a jpg image into the clipboard to paste into a document.
for example, past my signature into a word document without using word macros

I tried to save and load a captured area to a file and then load it. However, now every time I do a partial screen capture it overwrites the prior clip file, even without autohotkey running.

I'd love something simple. Basically insert c:\signature.jpg at cursor
suggestions?
Thanks,
Hal


Here's the code I had from another users suggestion:

Code: Select all

;USE THIS TO SAVE A CLIPBOARD IMAGE TO A FILE TO USE FOR PASTING WITH 2ND SCRIPT FOLLOWING THIS
;FIRST COPY AN IMAGE TO CLIPBOARD USING SNIPING TOOL ETC
#!'::        
;  win alt'
#Persistent

OnClipboardChange:
 if(A_EventInfo=2)   ; if the Clipboard contains images
{
;FileDelete, C:\image saved.clip
ToolTip image saved
FileAppend, %ClipboardAll%, d:\dropbox\_SIGNATURE FILES\CLIPautohotkey\MailSignOff.clip	  ; save image
Sleep 1000
ToolTip
}
return



;USE THIS TO LOAD & PASTE MailSignOFF TO CLIPBOARD 
;THIS ONE IS E-MAIL SIGNOFF
#+/::
 ; win-shft/
;ClipSaved := ClipboardAll
;Clipboard =
FileRead, Clipboard, *c d:\dropbox\_SIGNATURE FILES\CLIPautohotkey\MailSignoff.clip
ClipWait, 2
Send ^v
;clipboard := ClipSaved
return
[Mod edit: [code][/code] tags added]

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Paste a saved image into a document

Post by Scr1pter » 22 Apr 2020, 15:16

Hi,

I made something similar a while ago, by using gdip:

Code: Select all

#SingleInstance force
#include Gdip_All.ahk

F1::
picturePath = C:\Users\USER\Documents\AHK\picture.png ; Path to picture
Read_In_Picture(picturePath) ; Grafik in die Zwischenablage kopieren 
return

Read_In_Picture(picture)
{
  pToken := Gdip_Startup() ; Gdip-Instanz starten
  pBitmap := Gdip_CreateBitmapFromFile(picture) ; Grafik definieren
  Gdip_SetBitmapToClipboard(pBitmap) ; Grafik in die Zwischenablage kopieren PICTURE TO CLIPBOARD
  Gdip_DisposeImage(pBitmap) ; Grafiken aus Speicher entfernen
  Gdip_Shutdown(pToken) ; Gdip-Instanz schließen
}
After using that function, you should just paste the picture.
It should also work with jpg, but perhaps you can switch to png?

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Paste a saved image into a document

Post by flyingDman » 22 Apr 2020, 15:29

Basically insert c:\signature.jpg at cursor
Try:

Code: Select all

oWord := ComObjActive("Word.Application")
oWord.ActiveDocument.Shapes.AddPicture("c:\signature.jpg")
14.3 & 1.3.7

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Paste a saved image into a document

Post by teadrinker » 22 Apr 2020, 15:29

Code: Select all

OnClipboardChange:
if(A_EventInfo=2)   ; if the Clipboard contains images
{
FileDelete, d:\dropbox\_SIGNATURE FILES\CLIPautohotkey\MailSignOff.clip
ToolTip image saved
FileAppend, %ClipboardAll%, d:\dropbox\_SIGNATURE FILES\CLIPautohotkey\MailSignOff.clip
Sleep 1000
ToolTip
}
return 

#+/::
if !Word := GetActiveWord() {
   MsgBox, Failed to get active Word document
   Return
}
FileRead, Clipboard, *c d:\dropbox\_SIGNATURE FILES\CLIPautohotkey\MailSignOff.clip
Word.Selection.PasteSpecial()
Return

GetActiveWord()  {
   ControlGet, ControlHwnd, Hwnd,, _WwG1, % "ahk_id" WinExist("A")
   Return AccObjectFromWindow(ControlHwnd, -16).Application
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", Str, "oleacc", Ptr)
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", Ptr, hWnd, UInt, idObject, Ptr, &IID, PtrP, pAcc) = 0
      Return ComObjEnwrap(VT_DISPATCH, pAcc, F_OWNVALUE)
}

User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Paste a saved image into a document

Post by submeg » 30 Jan 2023, 15:52

Scr1pter wrote:
22 Apr 2020, 15:16
Hi,

I made something similar a while ago, by using gdip:

Code: Select all

#SingleInstance force
#include Gdip_All.ahk

F1::
picturePath = C:\Users\USER\Documents\AHK\picture.png ; Path to picture
Read_In_Picture(picturePath) ; Grafik in die Zwischenablage kopieren 
return

Read_In_Picture(picture)
{
  pToken := Gdip_Startup() ; Gdip-Instanz starten
  pBitmap := Gdip_CreateBitmapFromFile(picture) ; Grafik definieren
  Gdip_SetBitmapToClipboard(pBitmap) ; Grafik in die Zwischenablage kopieren PICTURE TO CLIPBOARD
  Gdip_DisposeImage(pBitmap) ; Grafiken aus Speicher entfernen
  Gdip_Shutdown(pToken) ; Gdip-Instanz schließen
}
After using that function, you should just paste the picture.
It should also work with jpg, but perhaps you can switch to png?

Cheers!
Hey @Scr1pter, I tried this, but when I press CTRL + V to paste, it says "the information on the clipboard can't be inserted into Paint."

My script is identical to yours:

Code: Select all


#SingleInstance force
#include C:\Users\PATH\AutoHotkey\Lib\Gdip_All.ahk

a::

PM_IconWaiting()

Return


PM_IconWaiting()
{

picturePath = C:\Users\PATH\AutoHotkey\Lib\Icons\JS_InfoWait.png

Read_In_Picture(picturePath) ; Grafik in die Zwischenablage kopieren 

}
Return

Read_In_Picture(picturePath)
{
  pToken := Gdip_Startup() ; Gdip-Instanz starten
  pBitmap := Gdip_CreateBitmapFromFile(picture) ; Grafik definieren
  Gdip_SetBitmapToClipboard(pBitmap) ; Grafik in die Zwischenablage kopieren PICTURE TO CLIPBOARD
  Gdip_DisposeImage(pBitmap) ; Grafiken aus Speicher entfernen
  Gdip_Shutdown(pToken) ; Gdip-Instanz schließen
}

Any ideas?
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Paste a saved image into a document

Post by ananthuthilakan » 31 Jan 2023, 01:29

Code: Select all

#include ImagePut (for v1).ahk ; https://github.com/iseahound/ImagePut
picturePath := "C:\Users\USER\Documents\AHK\picture.png"

F1::
ImagePutClipboard({image: picturePath, scale: ["500", "auto"]}) ; scale for width and height
; ImagePutClipboard(picturePath)  ; no scaling
sleep, 50
SendInput, ^{v}
return

Post Reply

Return to “Ask for Help (v1)”