#Requires AutoHotkey v1
url := "https://www.etonline.com/_next/image?url=https%3A%2F%2Fwww.etonline.com%2Fsites%2Fdefault%2Ffiles%2Fstyles%2F640x360%2Fpublic%2Fimages%2F2023-06%2FGettyImages-1258514215.jpg%3Fh%3D71ff8dab&w=1024&q=75"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", url, true)
whr.Send()
whr.WaitForResponse()
StreamToClipboard(whr.ResponseStream)
StreamToClipboard(Stream) {
static flags := (GMEM_MOVEABLE := 0x2) | (GMEM_ZEROINIT := 0x40)
, szBITMAP := 16 + A_PtrSize * 2, szBITMAPINFOHEADER := 40, CF_DIB := 0x8
, IID_IStream := "{0000000C-0000-0000-C000-000000000046}"
DllCall("Ole32\GetHGlobalFromStream", "Ptr", IStream := ComObjQuery(Stream, IID_IStream), "PtrP", pData)
DllCall("OpenClipboard", "Ptr", A_ScriptHwnd)
DllCall("EmptyClipboard")
pngFormat := DllCall("RegisterClipboardFormat", "Str", "PNG")
DllCall("SetClipboardData", "UInt", pngFormat, "Ptr", pData)
hBitmap := HBitmapFromStream(IStream)
VarSetCapacity(DIBSECTION, size := A_PtrSize = 4 ? 84 : 104, 0)
DllCall("GetObject", "Ptr", hBitmap, "Int", size, "Ptr", &DIBSECTION)
dataSize := NumGet(DIBSECTION, szBITMAP + 20, "UInt")
objSize := szBITMAPINFOHEADER + dataSize
hObj := DllCall("GlobalAlloc", "UInt", flags, "Ptr", objSize, "Ptr")
pObj := DllCall("GlobalLock", "Ptr", hObj, "Ptr")
DllCall("RtlMoveMemory", "Ptr", pObj, "Ptr", &DIBSECTION + szBITMAP, "Ptr", szBITMAPINFOHEADER)
DllCall("RtlMoveMemory", "Ptr", pObj + szBITMAPINFOHEADER, "Ptr", NumGet(DIBSECTION, 16 + A_PtrSize), "Ptr", dataSize)
NumPut(-NumGet(pObj + 8, "Int"), pObj + 8, "Int")
DllCall("SetClipboardData", "UInt", CF_DIB, "Ptr", hObj)
DllCall("CloseClipboard")
DllCall("DeleteObject", "Ptr", hBitmap)
ObjRelease(IStream)
}
HBitmapFromStream(IStream) {
static CLSID_WICImagingFactory := "{CACAF262-9370-4615-A13B-9F5539DA4C0A}"
, IID_IWICImagingFactory := "{EC5EC8A9-C395-4314-9C77-54D7A935FF70}"
, GUID_WICPixelFormat32bppBGRA := "{6FDDC324-4E03-4BFE-B185-3D77768DC90F}"
, opt := WICDecodeMetadataCacheOnDemand := 0
IWICImagingFactory := ComObjCreate(CLSID_WICImagingFactory, IID_IWICImagingFactory)
; IWICImagingFactory::CreateDecoderFromFilename
DllCall(NumGet(NumGet(IWICImagingFactory + 0) + A_PtrSize * 4), "Ptr", IWICImagingFactory, "Ptr", IStream
, "Ptr", 0, "UInt", opt, "PtrP", IWICBitmapDecoder)
; IWICBitmapDecoder::GetFrame
DllCall(NumGet(NumGet(IWICBitmapDecoder + 0) + A_PtrSize * 13), "Ptr", IWICBitmapDecoder, "UInt", 0, "PtrP", IWICBitmapFrameDecode)
; IWICBitmapFrameDecode::GetSize
DllCall(NumGet(NumGet(IWICBitmapFrameDecode + 0) + A_PtrSize * 3), "Ptr", IWICBitmapFrameDecode, "UIntP", width, "UIntP", height)
VarSetCapacity(GUID, 16, 0)
DllCall("Ole32\CLSIDFromString", "WStr", GUID_WICPixelFormat32bppBGRA, "Ptr", &GUID)
hr := DllCall("Windowscodecs\WICConvertBitmapSource", "Ptr", &GUID, "Ptr", IWICBitmapFrameDecode, "PtrP", DestBitmapFrameDecode)
if (hr = 0)
ObjRelease(IWICBitmapFrameDecode), IWICBitmapFrameDecode := DestBitmapFrameDecode
stride := width * 4, size := stride * height, VarSetCapacity(buf, size, 0)
; IWICBitmapFrameDecode::CopyPixels
DllCall(NumGet(NumGet(IWICBitmapFrameDecode + 0) + A_PtrSize * 7), "Ptr", IWICBitmapFrameDecode, "Ptr", 0, "UInt", stride, "UInt", size, "Ptr", &buf)
hBitmap := CreateDIBSection(width, height, pBits)
DllCall("RtlMoveMemory", "Ptr", pBits, "Ptr", &buf, "Ptr", size)
for k, v in [ IWICBitmapFrameDecode, IWICBitmapDecoder, IWICImagingFactory ]
ObjRelease(v)
return hBitmap
}
CreateDIBSection(w, h, ByRef ppvBits := 0, bpp := 32) {
hDC := DllCall("GetDC", "Ptr", 0, "Ptr")
VarSetCapacity(BITMAPINFO, 40, 0)
NumPut(40 , BITMAPINFO, 0)
NumPut( w , BITMAPINFO, 4)
NumPut(-h , BITMAPINFO, 8)
NumPut( 1 , BITMAPINFO, 12)
NumPut(bpp, BITMAPINFO, 14)
hBM := DllCall("CreateDIBSection", "Ptr", hDC, "Ptr", &BITMAPINFO, "UInt", 0
, "PtrP", ppvBits, "Ptr", 0, "UInt", 0, "Ptr")
DllCall("ReleaseDC", "Ptr", 0, "Ptr", hDC)
return hBM
}