Using Win32 API to create Quality of Life Automation Scripts

Using Win32  API to create Quality of Life Automation Scripts
; Set the destination path for the PNG file
destPngFilePath := A_ScriptDir . "\" . A_Now . ".png"

; Ctrl+Shift+O is the hotkey to trigger EVERYTHING
^+o:: 
    ; 1. Get bitmap from clipboard
    hBitmap := GetBitmapFromClipboard() 

    ; 2. Convert bitmap to PNG
    HBitmapToPng(hBitmap, destPngFilePath) 

    ; 3. Delete bitmap object
    DllCall("DeleteObject", "Ptr", hBitmap) 
    Return


GetBitmapFromClipboard() {

    ; Check if the clipboard contains a bitmap ( ANY IMAGE )
    if !DllCall("IsClipboardFormatAvailable", "UInt", CF_BITMAP := 2)
        throw "There is no image in the Clipboard"

    ; Open the clipboard
    if !DllCall("OpenClipboard", "Ptr", 0)
        throw "OpenClipboard failed"

    ; Get the handle to the bitmap
    hBitmap := DllCall("GetClipboardData", "UInt", CF_BITMAP)

    ; Close the clipboard
    DllCall("CloseClipboard")

    ; Check if bitmap handle is valid
    if !hBitmap
        throw "GetClipboardData failed"
    Return hBitmap
}



HBitmapToPng(hBitmap, destPngFilePath) {

    ; ALL THE static variables and constants

    static CLSID_WICImagingFactory := "{CACAF262-9370-4615-A13B-9F5539DA4C0A}"
         , IID_IWICImagingFactory  := "{EC5EC8A9-C395-4314-9C77-54D7A935FF70}"
         , GUID_ContainerFormatPng := "{1B7CFAF4-713F-473C-BBCD-6137425FAEAF}"
         , WICBitmapUseAlpha := 0x00000000, GENERIC_WRITE := 0x40000000
         , WICBitmapEncoderNoCache := 0x00000002




    ; Create GUID for PNG container format
    VarSetCapacity(GUID, 16, 0)
    DllCall("Ole32\CLSIDFromString", "WStr", GUID_ContainerFormatPng, "Ptr", &GUID)

    ; Create IWICImagingFactory object
    IWICImagingFactory := ComObjCreate(CLSID_WICImagingFactory, IID_IWICImagingFactory)

    ; Create IWICBitmap from HBITMAP
    Vtable(IWICImagingFactory, CreateBitmapFromHBITMAP := 21).Call("Ptr", hBitmap, "Ptr", 0, "UInt", WICBitmapUseAlpha, "PtrP", IWICBitmap)

    ; Create IWICStream object
    Vtable(IWICImagingFactory, CreateStream := 14).Call("PtrP", IWICStream)

   ; Initialize IWICStream from filename
    Vtable(IWICStream, InitializeFromFilename := 15).Call("WStr", destPngFilePath, "UInt", GENERIC_WRITE)

    ; Create IWICBitmapEncoder object
    Vtable(IWICImagingFactory, CreateEncoder := 8).Call("Ptr", &GUID, "Ptr", 0, "PtrP", IWICBitmapEncoder)

    ; Initialize IWICBitmapEncoder
    Vtable(IWICBitmapEncoder, Initialize := 3).Call("Ptr", IWICStream, "UInt", WICBitmapEncoderNoCache)

    ; Create new frame for encoding
    Vtable(IWICBitmapEncoder, CreateNewFrame := 10).Call("PtrP", IWICBitmapFrameEncode, "Ptr", 0)




    Vtable(IWICBitmapFrameEncode, Initialize := 3).Call("Ptr", 0)
    ; Write source bitmap to frame
    Vtable(IWICBitmapFrameEncode, WriteSource := 11).Call("Ptr", IWICBitmap, "Ptr", 0)




    ; Commit IWICBitmapFrameEncode
    Vtable(IWICBitmapFrameEncode, Commit := 12).Call()
    ; Commit IWICBitmapEncoder
    Vtable(IWICBitmapEncoder, Commit := 11).Call()


    ; Release COM objects
    for k, v in [IWICBitmapFrameEncode, IWICBitmapEncoder, IWICStream, IWICBitmap, IWICImagingFactory]
        ObjRelease(v)
}


Vtable(ptr, n) {
    return Func("DllCall").Bind(NumGet(NumGet(ptr+0), A_PtrSize*n), "Ptr", ptr)
}


; GPT + AHK Community Code  - Works like MAGIC !!

This AutoHotkey v1 script captures an image from the clipboard and saves it as a PNG file when pressing Ctrl + Shift + O. { you can change this to whatever hotkey combination you want }

  1. Hotkey Binding: The script binds the Ctrl + Shift + O combination to execute the following code when pressed.
; Ctrl+Shift+O hotkey
^+o:: 
    ; The 3 steps    
    Return
  1. GetBitmapFromClipboard() : Retrieves the bitmap image data from the clipboard.

  2. HBitmapToPng(): Converts the bitmap image data to PNG format and saves it to a specified file path.

  3. Using Win32 API Functions: The script utilizes many Win32 API functions through the DllCall function to interact with the clipboard and perform image conversion.

  4. COM Object Creation: It creates COM objects to handle image conversion using the Windows Imaging Component (WIC). This includes creating an instance of the IWICImagingFactory interface to facilitate image encoding.

    •       ; Create IWICImagingFactory object
                IWICImagingFactory := ComObjCreate(CLSID_WICImagingFactory, IID_IWICImagingFactory)
      
  5. Converting Bitmap to PNG: The script follows a series of steps to encode the bitmap image as PNG format.

    1. Initializes an encoder

    2. Creates a new frame for encoding

    3. Writes the source bitmap to the frame

    4. Commits the encoding process

  6. Memory Management: It ensures proper memory management by releasing COM objects after they are no longer needed. ( yeah it's super clean code ! )

  7. DLL (Dynamic Link Library) Calls:

    • DLLs are files containing code and data that can be used by multiple programs simultaneously.

    • DllCall() is an AutoHotkey function used to call functions exported by DLL files.

    • It allows AutoHotkey scripts to access functionality not directly provided by the language itself, such as interacting with the clipboard or performing low-level operations.

  8. Bitmap Images:

    • Bitmap (BMP) images are a common raster graphics format used to store digital images.

    • They consist of a grid of pixels, where each pixel contains information about its color.

    • In our script, bitmap images are retrieved from the clipboard and converted to PNG format for saving.

  9. Windows Imaging Component (WIC):

    • The Windows Imaging Component is a Windows API for working with digital images.

    • It provides a set of COM (Component Object Model) interfaces for image encoding, decoding, and processing.

    • In our script, COM objects representing WIC interfaces are created to perform image conversion operations.

  10. COM (Component Object Model):

    • COM is a binary-interface standard for software components introduced by Microsoft.

    • It defines how components communicate with each other and interact within the same process or across different processes.

    • In our script, COM objects are used to interact with WIC interfaces for image conversion.

  11. PNG (Portable Network Graphics) :

    • PNG, apart from what we know as the extension of images, is a popular lossless image format widely used for storing digital images.

    • It supports transparency and is well-suited for web graphics and other applications.

    • In our script, bitmap images retrieved from the clipboard are converted to PNG format for saving to disk.

This script provides a seamless way to capture images from the clipboard and save them as PNG files. All this at the stroke of a single simple keyboard shortcut. It leverages Win32 API functions and COM objects to achieve efficient image conversion and file saving.

Thank you,

__CPP_Try_Hard__ ;