Jump to content
Sign in to follow this  
John

[SOLVED] Getting handle to clipboard image

Recommended Posts

John

Previous relevant thread:

The idea is to get save the clipboard image to file. Saving from a screen capture works fine like this:

Func _CapureImage()
    Local $hBitmap, $hImage
    _GDIPlus_Startup ()
    $hBitmap = _ScreenCapture_Capture ("")
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)
    _GDIPlus_ImageSaveToFile ($hImage, @ScriptDir & "\XXX.jpg")
    _GDIPlus_ImageDispose ($hImage)
    _WinAPI_DeleteObject ($hBitmap)
    _GDIPlus_ShutDown ()
EndFunc

This does not work:

Func _SaveClip()
$hGUI=GUICreate("SaveClip")
 _GDIPlus_Startup ()
_ClipBoard_Open($hGUI)
$re=ClipGet()
    if @error = 2 Then
        $hBitmap=_ClipBoard_GetData($CF_BITMAP)
        $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)
        _GDIPlus_ImageSaveToFile($hImage,@ScriptDir&"\XXX.jpg")
        _GDIPlus_ImageDispose ($hImage)
        _WinAPI_DeleteObject ($hBitmap)
    EndIf
        _GDIPlus_ShutDown ()
EndFunc

The problem appears to be the _ClipBoard_GetData($CF_BITMAP) function is not returning a handle, regardless of what kind of data is on the clipboard. It always returns 0. I have also tried using _ClipBoard_GetDataEx(), which is simply a DllCall, with the same result.

How do I get get a handle to a clipboard image, $hBitmap, that I can then save to a file?

Edited by John

Share this post


Link to post
Share on other sites
John

Board went offline for maintenance after posting this morning. Anybody have any idea how to get a handle to a clipboard image, or why $hBitmap=_ClipBoard_GetData($CF_BITMAP) fails to return it?

Share this post


Link to post
Share on other sites
rover

Board went offline for maintenance after posting this morning. Anybody have any idea how to get a handle to a clipboard image, or why $hBitmap=_ClipBoard_GetData($CF_BITMAP) fails to return it?

You can't use _ClipBoard_GetData() for bitmaps,

because of how it is coded internally, use _ClipBoard_GetDataEx()

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

_SaveClip(@ScriptDir & "\XXX.jpg")

Func _SaveClip($sFile)
    If _ClipBoard_IsFormatAvailable($CF_BITMAP) <> 1 Then Return SetError(1, 0, 0)
    If _ClipBoard_Open(0) <> 1 Then Return SetError(2, 0, 0)
    If @error Then SetError(3, 0, 0)
    $hBitmap = _ClipBoard_GetDataEx($CF_BITMAP)
    If IsPtr($hBitmap) = 0 Then Return SetError(4, 0, 0)
    _GDIPlus_Startup()
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    If @error Or IsPtr($hImage) = 0 Then
        _ClipBoard_Close()
        _WinAPI_DeleteObject($hBitmap)
        _GDIPlus_Shutdown()
        Return SetError(5, 0, 0)
    EndIf
    _ClipBoard_Close()
    _WinAPI_DeleteObject($hBitmap)
    If _GDIPlus_ImageSaveToFile($hImage, $sFile) = False Then
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_Shutdown()
        Return SetError(6, 0, 0)
    Else
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_Shutdown()
        Return SetError(0, 0, 1)
    EndIf
EndFunc   ;==>_SaveClip

I see fascists...

Share this post


Link to post
Share on other sites
John

Oh cool. Thanks Problem solved.

Sorry for the late response, but was out for a few days.

Thanks.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.