Jump to content

[SOLVED] Getting handle to clipboard image


John
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

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