Jump to content

Copy bitmap to the clipboard succeed but doesn't work


Go to solution Solved by ioa747,

Recommended Posts

Try to copy the bitmap of a image file to clipboard then paste into MS Paint, but Paint doesn't recognize the data and show an error. In fact Paint even freeze for a while.

$rst is not 0, meaning _ClipBoard_SetDataEx succeed, but the image is not in the clipboard viewer and can't be paste into other program. 

Here is the code:

#include <Constants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIHObj.au3>
#include <File.au3>
#include <Clipboard.au3>
 
_GDIPlus_Startup()
_ClipBoard_Open(0)
Global $hBmp = _GDIPlus_BitmapCreateFromFile("C:\Users\olnw\Desktop\123.jpg")
$hImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)
$rst = _ClipBoard_SetDataEx($hImage, $CF_BITMAP)
MsgBox(0,$rst,$rst)
;_GDIPlus_BitmapDispose($hImage)
;_WinAPI_DeleteObject($hBmp)
;_GDIPlus_Shutdown()
;_ClipBoard_Close()
 
Paint error message:
2023_04_17_14_08_11.jpg.4489d4ff4d90f6b6506243da55186b79.jpg
 
Link to comment
Share on other sites

#include <GDIPlus.au3>
#include <Clipboard.au3>

_GDIPlus_Startup()
_ClipBoard_Open(0)

Local $hBmp = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\test.png")
Local $hImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)

Local $res = _ClipBoard_SetDataEx(_WinAPI_CopyImage($hImage), $CF_BITMAP)

_WinAPI_DeleteObject($hImage)
_GDIPlus_BitmapDispose($hBmp)

_ClipBoard_Close()
_GDIPlus_Shutdown()

 

Link to comment
Share on other sites

 

Caused by the "ByRef" of $hMemory in _ClipBoard_SetDataEx(), design decision and should not change the functionality. Not sure why it throws an error for you, and not for me 🙄. Maybe because I run the latest Beta of Au3Check.exe?

#include <GDIPlus.au3>
#include <Clipboard.au3>

_GDIPlus_Startup()
_ClipBoard_Open(0)

Global $hBmp = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\test.png")
Global $hImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)

Global $hImage_Copy = _WinAPI_CopyImage($hImage)
Global $res = _ClipBoard_SetDataEx($hImage_Copy, $CF_BITMAP)

_WinAPI_DeleteObject($hImage)
_GDIPlus_BitmapDispose($hBmp)

_ClipBoard_Close()
_GDIPlus_Shutdown()

 

 

Link to comment
Share on other sites

NOT a good idea to put _WinAPI_CopyImage as a parameter or simply said : not with a delete object.  Eventually this will create a large memory leak.  Like the Beatles said : Dear Prudence

Link to comment
Share on other sites

Object is handed to system clipboard and must not be deleted by script.

 

Edit: Run this and check with task-manager

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GDIPlus.au3>
#include <Clipboard.au3>

For $i = 1 To 1000

    Sleep(1000)

    _GDIPlus_Startup()
    _ClipBoard_Open(0)

    Global $hBmp = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\test.png")
    Global $hImage = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp)

    Global $hImage_Copy = _WinAPI_CopyImage($hImage)

    ConsoleWrite(@AutoItPID & @tab & $i & @tab & $hImage_Copy & @crlf)

    Global $res = _ClipBoard_SetDataEx($hImage_Copy, $CF_BITMAP)

    _WinAPI_DeleteObject($hImage)
    _GDIPlus_BitmapDispose($hBmp)

    _ClipBoard_Close()
    _GDIPlus_Shutdown()

Next

 

Edited by KaFu
Link to comment
Share on other sites

Good catch @KaFu.  Per help file on _ClipBoard_SetDataEx.

Quote
If this function succeeds, the system owns the object identified by the $hMemory parameter.
The application may not write to or free the data once ownership has been transferred to the system

:)

Link to comment
Share on other sites

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...