Jump to content

Images and the Clipboard


ProgAndy
 Share

Recommended Posts

I made a UDF to copy Images to the Clipboard. You can use all formats of GDIPlus, but Transparency is not supported.

ImageClip.au3:

#include <ClipBoard.au3>
#include <GDIPlus.au3>

;===============================================================================
;
; Function Name:   _ImageToClip
; Description::    Copies all Image Files to ClipBoard
; Parameter(s):    $Path -> Path of image
; Requirement(s):  GDIPlus.au3
; Return Value(s): Success: 1
;                  Error: 0 and @error:
;                          1 -> Error in FileOpen
;                          2 -> Error when setting to Clipboard
; Author(s):       
;
;===============================================================================
;
Func _ImageToClip($Path)
    _GDIPlus_Startup()
    Local $hImg = _GDIPlus_ImageLoadFromFile($Path)
    If $hImg = 0 Then Return SetError(1,0,0)
    Local $hBitmap = _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
    _GDIPlus_ImageDispose($hImg)
    _GDIPlus_Shutdown()
    Local $ret = _ClipBoard_SetHBITMAP($hBitmap)
    Return 1
EndFunc 

;===============================================================================
;
; Function Name:   _ClipBoard_SetHBITMAP
; Description::    Sets a HBITAMP as ClipBoardData
; Parameter(s):    $hBitmap -> Handle to HBITAMP from GDI32, NOT GDIPlus
; Requirement(s):  ClipBoard.au3
; Return Value(s): Success: 1 ; Error: 0 And @error = 1
; Author(s):       Prog@ndy
; Notes:           To use Images from GDIplus, convert them with _GDIPlus_ImageCreateGDICompatibleHBITMAP
;
;===============================================================================
;
Func _ClipBoard_SetHBITMAP($hBitmap,$Empty = 1)
    _ClipBoard_Open(_AutoItWinGetHandle())
    If $Empty Then _ClipBoard_Empty()
    _ClipBoard_SetDataEx( $hBitmap, $CF_BITMAP)
    _ClipBoard_Close()
    If Not _ClipBoard_IsFormatAvailable($CF_BITMAP)  Then
        Return SetError(1,0,0)
    EndIf
EndFunc

;===============================================================================
;
; Function Name:   _GDIPlus_ImageCreateGDICompatibleHBITMAP
; Description::    Converts a GDIPlus-Image to GDI-combatible HBITMAP
; Parameter(s):    $hImg -> GDIplus Image object
; Requirement(s):  GDIPlus.au3
; Return Value(s): HBITMAP, compatible with ClipBoard
; Author(s):       Prog@ndy
;
;===============================================================================
;

Func _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
    Local $hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImg)
    Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+$LR_COPYRETURNORG)
    _WinAPI_DeleteObject($hBitmap2)
    Return $hBitmap
EndFunc

;===============================================================================
;
; Function Name:   _WinAPI_CopyImage
; Description::    Copies an image, also makes GDIPlus-HBITMAP to GDI32-BITMAP
; Parameter(s):    $hImg -> HBITMAP Object, GDI or GDIPlus
; Requirement(s):  WinAPI.au3
; Return Value(s): Succes: Handle to new Bitmap, Error: 0
; Author(s):       Prog@ndy
;
;===============================================================================
;
Func _WinAPI_CopyImage($hImg,$uType=0,$x=0,$y=0,$flags=0)
    Local $aResult

    $aResult = DllCall("User32.dll", "hwnd", "CopyImage", "hwnd", $hImg,"UINT",$uType,"int",$x,"int",$y,"UINT",$flags)
    _WinAPI_Check("_WinAPI_CopyImage", ($aResult[0] = 0), 0, True)
    Return $aResult[0]
EndFunc   ;==>_WinAPI_CopyIcon

;===============================================================================
;
; Function Name:   _AutoItWinGetHandle
; Description::    Returns the Windowhandle of AutoIT-Window
; Parameter(s):    --
; Requirement(s):  --
; Return Value(s): Autoitwindow Handle
; Author(s):       Prog@ndy
;
;===============================================================================
;
Func _AutoItWinGetHandle()
    Local $oldTitle = AutoItWinGetTitle()
    Local $x = Random(1248578,1249780)
    AutoItWinSetTitle("qwrzu"&$x)
    Local $x = WinGetHandle("qwrzu"&$x)
    AutoItWinSetTitle($oldTitle)
    Return $x
EndFuncoÝ÷ ØLZ^jëh×6; #####################################################################
; #####################################################################
;   EXAMPLE
; #####################################################################

    #include "ImageClip.au3"

    $Path = "C:\WINDOWS\Zapotek.bmp"
    _ImageToClip($Path)
    $paint = Run("mspaint")
    $oldMatch = Opt("WinTitleMatchMode",2)
    WinWait("- Paint")
    Opt("WinTitleMatchMode",$oldMatch)
    If WinExists("Untitled - Paint") Then
        WinWaitActive("Untitled - Paint")
    Else
        WinWaitActive("Unbenannt - Paint")
    EndIf
    Send("^v")
; #####################################################################
; #####################################################################

In the attachment, Example and UDF are in one file :)

//Edit: Had to reforamt :(

ImageClip.au3

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Your collection of created functions, UDF, are going to make life easier when the need arises.

For me, they will be doing the job until they die, or something better comes along. (Quote from movie "?????? Troopers")

Really great

Thanks for sharing

Link to comment
Share on other sites

I made a new example:

; #####################################################################
; #####################################################################
;   EXAMPLE
; #####################################################################
    #include <File.au3>
    $files = _FileListToArray(@ScriptDir & "\pics","*.*")
;~  $Path = "D:\Dokumente\Dateien von Andreas\Eigene Bilder\Avatar-MR-X.gif"
    $paint = Run("mspaint")
    $oldMatch = Opt("WinTitleMatchMode",2)
    WinWait("- Paint")
    Opt("WinTitleMatchMode",$oldMatch)
    If WinExists("Untitled - Paint") Then
        WinWaitActive("Untitled - Paint")
    Else
        WinWaitActive("Unbenannt - Paint")
    EndIf
    For $i = 1 To $files[0]
        ToolTip($files[$i])
        Sleep(100)
        $Path = @ScriptDir & "\pics\" & $files[$i]
        _ImageToClip($Path)
        If @error = 1 Then ToolTip($files[$i] & @CRLF & " Error: 1 File could not be loaded. Does not Exist or is no Image file")
        Send("^v")
        Sleep(2000)
        Send("^a")
        Sleep(100)
        Send("{DEL}")
        Sleep(500)
        ToolTip("")
        ClipPut("")
    Next
; #####################################################################
; #####################################################################

It loads different Image formats, so you can check, wich work.

The images are loaded from @ScriptDir\pics\

I attached some examle pics.

//Edit: Too big. Load HIER

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Your collection of created functions, UDF, are going to make life easier when the need arises.

For me, they will be doing the job until they die, or something better comes along. (Quote from movie "?????? Troopers")

Really great

Thanks for sharing

Starship Trooper

I need a corporal. You're it, until you're dead or I find someone better.

http://www.imdb.com/title/tt0120201/quotes
Link to comment
Share on other sites

  • 3 years later...

_WinAPI_Check does not exist.. i tried to look to see if there was a replacement for it under script breaking changes

http://www.autoitscript.com/autoit3/docs/script_breaking_changes.htm

couldnt find a trace of it. it also doesnt exist in the UDF.. where can i get it?

Link to comment
Share on other sites

@gcue

Maybe this can get you going.

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_Check
; Description ...: Displays any errors produced by the AutoIt library
; Syntax.........: _WinAPI_Check($sFunction, $fError, $vError[, $fTranslate = False])
; Parameters ....: $Function     - Name of function
;                 $fError      - True if error occurred
;                 $iError      - Error code
;                 $fTranslate   - Translate error using _WinAPI_GetLastErrorMessage
; Return values .:
; Author ........: Paul Campbell (PaulIA)
; Modified.......:
; Remarks .......: This function is used within many of the lower level API functions to simulate a  "fatal"  application  error.
;                 It is used in functions that normally should not fail or where there is no clear corrective action that can be
;                 taken if an error is generated.
; Related .......: _WinAPI_GetLastErrorMessage
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _WinAPI_Check($sFunction, $fError, $vError, $fTranslate = False)
If $fError Then
  If $fTranslate Then $vError = _WinAPI_GetLastErrorMessage()
  _WinAPI_ShowError($sFunction & ": " & $vError)
EndIf
EndFunc   ;==>_WinAPI_Check
Link to comment
Share on other sites

  • 1 year later...

I have tried to use this but get the following error:

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:SynergyExtraReadsExtraReads2.au3" /UserParams   
+>12:04:32 Starting AutoIt3Wrapper v.2.1.2.9    Environment(Language:0409  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X86)
>Running AU3Check (1.54.22.0)  from:C:Program FilesAutoIt3
C:\Program Files\AutoIt3\Include\ImageClip.au3(65,70) : WARNING: $LR_COPYDELETEORG: possibly used before declaration.
 Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Include\ImageClip.au3(65,88) : WARNING: $LR_COPYRETURNORG: possibly used before declaration.
 Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+$LR_COPYRETURNORG)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Include\ImageClip.au3(65,70) : ERROR: $LR_COPYDELETEORG: undeclared global variable.
 Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Include\ImageClip.au3(65,88) : ERROR: $LR_COPYRETURNORG: undeclared global variable.
 Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+$LR_COPYRETURNORG)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Include\ImageClip.au3(85,63) : ERROR: _WinAPI_Check(): undefined function.
 _WinAPI_Check("_WinAPI_CopyImage", ($aResult[0] = 0), 0, True)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:SynergyExtraReadsExtraReads2.au3 - 3 error(s), 2 warning(s)
!>12:04:33 AU3Check ended. Press F4 to jump to next error.rc:2
>Exit code: 2    Time: 2.009

I have imageclip.au3 in my include folder.

I also added winapiex.au3 to the include. imageclip.au3 doesn't have it or winapi.au3 on the includes at the top of the script but it seems that is where these errors have their root. I am not sure if I should try modifying imageclip.au3 to put these in the includes or not?

 

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...