how can i with pressing 1 load a image in to Clipboard and Paste it?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
John1
Posts: 236
Joined: 11 May 2020, 11:54

how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by John1 » 13 Oct 2020, 03:33

Hello,

how can i with pressing 1 load a image in to Clipboard and Paste it?

Image:
D:\0 BALDI VS GRANNY\129 Amoung Us 1\Render\Video\4_A6.png

Code: Select all

1::

sleep,500
Send ^v 
return


Thank you!

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 13 Oct 2020, 06:30

Code: Select all

1::
imageFile = D:\0 BALDI VS GRANNY\129 Amoung Us 1\Render\Video\4_A6.png
Clipboard =
FileToClipboard(imageFile)
ClipWait, 2
Sleep, 500
Send ^v
Return

FileToClipboard(PathToCopy, Method := "copy") {
 ; https://autohotkey.com/board/topic/23162-how-to-copy-a-file-to-the-clipboard/page-4
 FileCount := PathLength := 0
 Loop, Parse, PathToCopy, `n, `r ; Count files and total string length
  FileCount++, PathLength+=StrLen(A_LoopField)
 pid := DllCall("GetCurrentProcessId", "uint"), hwnd := WinExist("ahk_pid " . pid)
 ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
 hPath := DllCall("GlobalAlloc", "uint", 0x42, "uint", 20 + (PathLength + FileCount + 1) * 2, "UPtr")
 pPath := DllCall("GlobalLock", "UPtr", hPath)
 NumPut(20, pPath+0), pPath += 16 ; DROPFILES.pFiles = offset of file list
 NumPut(1, pPath+0), pPath += 4 ; fWide = 0 -->ANSI, fWide = 1 -->Unicode
 Offset := 0
 Loop, Parse, PathToCopy, `n, `r ; Rows are delimited by linefeeds (`r`n)
  offset += StrPut(A_LoopField, pPath+offset, StrLen(A_LoopField)+1, "UTF-16") * 2
 DllCall("GlobalUnlock", "UPtr", hPath), DllCall("OpenClipboard", "UPtr", hwnd), DllCall("EmptyClipboard")
 DllCall("SetClipboardData", "uint", 0xF, "UPtr", hPath) ; 0xF = CF_HDROP
 ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
 ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
 mem := DllCall("GlobalAlloc", "uint", 0x42, "uint", 4, "UPtr"), str := DllCall("GlobalLock", "UPtr", mem)
 If !(Method ~= "copy|cut") {
  DllCall("CloseClipboard")
  Return
 } Else DllCall("RtlFillMemory", "UPtr", str, "uint", 1, "UChar", (Method = "copy" ? "0x05" : "0x02"))
 DllCall("GlobalUnlock", "UPtr", mem)
 cfFormat := DllCall("RegisterClipboardFormat", "Str", "Preferred DropEffect")
 DllCall("SetClipboardData", "uint", cfFormat, "UPtr", mem)
 DllCall("CloseClipboard")
}

MrDoge
Posts: 151
Joined: 27 Apr 2020, 21:29

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by MrDoge » 13 Oct 2020, 10:07

if you meant this :
How do I copy an image file as only an image to the clipboard? - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/122225-how-do-i-copy-an-image-file-as-only-an-image-to-the-clipboard/

GDI+ standard library 1.45 by tic - AutoHotkey Community
https://www.autohotkey.com/boards/viewtopic.php?t=6517

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 13 Oct 2020, 10:13

Looks nice, but for some reason, that method did not work when I tried it, which is why I provided the other code instead. I was probably doing something the wrong way.

MrDoge
Posts: 151
Joined: 27 Apr 2020, 21:29

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by MrDoge » 13 Oct 2020, 11:38

I renamed Gdip_All.ahk to Gdip.ahk
and I have Gdip.ahk in lib

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 13 Oct 2020, 11:53

Thanks. This may help. I think I was not accessing Gdip the right way.

Edit: worked!

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

SetClipboardBitmap

Post by SKAN » 16 Oct 2020, 04:52

Setting a bitmap to clipboard is simple and shouldn't need any library.

Code: Select all

SetClipboardBitmap( Filename ) {            ; By SKAN on D3AG @ tiny.cc/setclipboardbitmap
Local  HBM := 0,  RES := 0,  OCB := 0,  CF_BITMAP := 2
  If ( HBM := LoadPicture(Filename) )
  If ( HBM := DllCall("CopyImage", "Ptr",HBM, "Int",0, "Int",0, "Int",0, "Int",8, "Ptr") )
  If ( OCB := DllCall("OpenClipboard", "Ptr",A_ScriptHwnd) )
  If DllCall("EmptyClipboard")
     RES := DllCall("SetClipboardData", "Int",CF_BITMAP, "Ptr",HBM)
  OCB := OCB ? DllCall("CloseClipboard")*0 : 0
  HBM := HBM ? DllCall("DeleteObject", "Ptr",HBM)*0 : 0
Return !!RES
}
My Scripts and Functions: V1  V2

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 16 Oct 2020, 07:09

It doesn't look simple to me-- but works! Thanks.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by SKAN » 16 Oct 2020, 07:53

mikeyww wrote:
16 Oct 2020, 07:09
It doesn't look simple to me
When you see DllCall()'s without any structures involved, then it is fairly simple. :)
Also CF_BITMAP is deprecated and the OS will automatically (Vista+) create both CF_DIB and CF_DIBV5 when a CF_BITMAP is supplied.
In other words: This is a simple function that makes the OS do the work.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 16 Oct 2020, 08:26

Thanks for sharing all of this.

gyro2222
Posts: 15
Joined: 06 Sep 2017, 11:12

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by gyro2222 » 18 Apr 2021, 13:39

I copied the code and inserted my bmp location, but I get an error on compiling. Help please.

Code: Select all

#+.::
SetClipboardBitmap( D:\TT-AF Dropbox\hal myers\_SIGNATURE FILES\ Hals signature1-blue.bmp) 
 { Local  HBM := 0,  RES := 0,  OCB := 0,  CF_BITMAP := 2
  If ( HBM := LoadPicture( D:\TT-AF Dropbox\hal myers\_SIGNATURE FILES\ Hals signature1-blue.bmp) )
  If ( HBM := DllCall("CopyImage", "Ptr",HBM, "Int",0, "Int",0, "Int",0, "Int",8, "Ptr") )
  If ( OCB := DllCall("OpenClipboard", "Ptr",A_ScriptHwnd) )
  If DllCall("EmptyClipboard")
     RES := DllCall("SetClipboardData", "Int",CF_BITMAP, "Ptr",HBM)
  OCB := OCB ? DllCall("CloseClipboard")*0 : 0
  HBM := HBM ? DllCall("DeleteObject", "Ptr",HBM)*0 : 0
Return !!RES
}

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 18 Apr 2021, 15:10

Functions accept expressions, such as quoted strings.

Code: Select all

#+.::SetClipboardBitmap("D:\TT-AF Dropbox\hal myers\_SIGNATURE FILES\ Hals signature1-blue.bmp")

SetClipboardBitmap( Filename ) {            ; By SKAN on D3AG @ tiny.cc/setclipboardbitmap
 Local  HBM := 0,  RES := 0,  OCB := 0,  CF_BITMAP := 2
 If !FileExist(Filename) {
   MsgBox, 48, Error, File not found. Aborting.`n`n%Filename%
   Return
 }
 If ( HBM := LoadPicture(Filename) )
  If ( HBM := DllCall("CopyImage", "Ptr",HBM, "Int",0, "Int",0, "Int",0, "Int",8, "Ptr") )
   If ( OCB := DllCall("OpenClipboard", "Ptr",A_ScriptHwnd) )
    If DllCall("EmptyClipboard")
     RES := DllCall("SetClipboardData", "Int",CF_BITMAP, "Ptr",HBM)
 OCB := OCB ? DllCall("CloseClipboard")*0 : 0
 HBM := HBM ? DllCall("DeleteObject", "Ptr",HBM)*0 : 0
 Return !!RES
}
Explained: Expressions

tawaha
Posts: 1
Joined: 28 Sep 2021, 01:14

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by tawaha » 28 Sep 2021, 02:36

Thanks Mr. Mikeyww for your code, it works like a Charm :dance:

Flowgun
Posts: 74
Joined: 25 Aug 2022, 09:42

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by Flowgun » 22 Apr 2023, 10:14

These functions don't support Webp format. Anyone knows how to get them to work with Webp?

MrDoge
Posts: 151
Joined: 27 Apr 2020, 21:29

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by MrDoge » 22 Apr 2023, 11:01

when copying webm image (right click) -> "Copy image" using chrome, it's converted to png
I check using this:

Code: Select all

<textarea id="textarea"></textarea>
<script>
    const textarea = document.getElementById("textarea")

    textarea.addEventListener("paste", event=>{
        var items = event.clipboardData.items;
        console.log(items)
        console.log(JSON.stringify(items)); // might give you mime types
        for (var index in items) {
            var item = items[index];
            console.log("kind", item.kind)
            if (item.kind === 'file') {
                var blob = item.getAsFile();
                var reader = new FileReader();
                reader.onload = function (event) {
                    console.log(event.target.result); // data url!
                };
                reader.readAsDataURL(blob);
            } else if (item.kind === 'string') {
                item.getAsString(data=>{
                    console.log(data)
                })
            }
        }
    })
</script>
Firefox does it correctly, "Copy Image" copies webp into clipboard
I don't know why Gdip is needed, just put the file in the clipboard

I'll edit this with an ahk solution, preferably no libraries

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 22 Apr 2023, 11:12

Code: Select all

#Requires AutoHotkey v1.1.33
#Include d:\Q\vis2\lib\Gdip_All.ahk ; https://github.com/marius-sucan/AHK-GDIp-Library-Compilation

picsDir := StrReplace(A_Desktop, "Desktop", "Pictures")
image   := picsDir "\abc 3.webp"

F3::imgToClipboard(image)

imgToClipboard(imagePath){
 pToken  := Gdip_Startup()
 hBitmap := HBitmapFromWebP(imagePath, width, height)
 Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap))
 Gdip_DisposeImage(pBitmap)
 Gdip_Shutdown(pToken)
 SoundBeep 1500
}
Plug in the rest from teadrinker's library: viewtopic.php?p=287344#p287344

MrDoge
Posts: 151
Joined: 27 Apr 2020, 21:29

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by MrDoge » 22 Apr 2023, 12:08

this is "filesToClipboard.ah2" (instead of imgToClipboard), but it works when pasting into discord (both images are shown, and with filename), I don't know where else to test
tell me if it doesn't work somewhere, then I'll know what to do (what format it(the app) checks)

Code: Select all

#SingleInstance force
ListLines 0
KeyHistory 0
SendMode "Input" ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir A_ScriptDir ; Ensures a consistent starting directory.

filesToClipboard(filePathsArr) {
    totalSize:=11 + filePathsArr.Length ;filePathsArr.Length for 0 terminated strings, 10 for 20, 1 for 0x0000 ending of string array
    for filePath in filePathsArr {
        totalSize+=StrLen(filePath) ;AHK String is internally UTF-16, I assume
    }
    totalSize*=2
    buf:=Buffer(totalSize)
    NumPut("UInt64",0x0000000000000014,buf,0x0)
    NumPut("UInt64",0x0000000000000000,buf,0x8)
    NumPut("UChar",0x00000001,buf,0x10)
    pointer:=buf.Ptr + 20
    for filePath in filePathsArr {
        pointer+=StrPut(filePath,pointer,"UTF-16")
    }
    NumPut("UShort",0x0000,pointer)
    ;buf filled
    DllCall("OpenClipboard", "Ptr",0)
    DllCall("EmptyClipboard")
    DllCall("SetClipboardData","Uint",15,"Ptr",buf) ;15=CF_HDROP
    DllCall("CloseClipboard")
}

filesToClipboard(["C:\Users\User\Downloads\img1.webp","C:\Users\User\Downloads\img2.webp"])

Exitapp

f3::Exitapp

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 22 Apr 2023, 12:38

It's a v2 script and does not work in Microsoft Word.

MrDoge
Posts: 151
Joined: 27 Apr 2020, 21:29

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by MrDoge » 22 Apr 2023, 12:46

MS Word doesn't support webp, pasting works with png
in MS Word you can import webp (by clicking buttons), but it converts it to its internal format ? which I think is png ?
so might as well convert it to png, as webp can never really exist in Word
but at the same time, I want the original webp to remain, for apps that do support it

filesToClipboard() only works in chrome, doesn't work in firefox, I'll have to think of something else

edit:
I tried "Copy Image" on webp from firefox to Word, didn't work, result: blank small image

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: how can i with pressing 1 load a image in to Clipboard and Paste it?

Post by mikeyww » 22 Apr 2023, 12:57

I believe that you are mistaken, because the clipboard is the clipboard. If it contains an image, then it can be pasted wherever an image can be pasted. After an image is copied to the clipboard, its original file format is irrelevant. The script that I posted demonstrates this. It sets the clipboard to a bitmap. At this point, the original file has no direct connection to the clipboard. Pasting and importing are different actions.

Post Reply

Return to “Ask for Help (v1)”