Sign in to follow this  
Followers 0
qwert

How to get handle of clipboard contents?

7 posts in this topic

I'm trying to write the current contents of the windows clipboard (a bitmap) to a file using:

#Include <ScreenCapture.au3>
_ScreenCapture_SaveImage($sFileName, $hBitmap[, $fFreeBmp = True])

My purpose is to capture a screen image that the user has already grabbed using PrintScreen. The problem is that I can't find a way to get the handle of the image that's on the clipboard. I've tried each of these these:

$hBitmap = _ClipBoard_GetDataEx($CF_Bitmap)
$hBitmap = _ClipBoard_GetDataEx($CF_MetaFilePict)   .....  where:
Return Value =
Success: Handle to a clipboard object in the specified format
Failure: 0

Zero is always returned. And when I changed to $CF_Text just as a test, AutoIt3 aborted.

Am I completely off base in trying it this way? I haven't found a simple example on these forums, so I'm beginning to think this isn't the right approach.

Thanks for any help with this.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This works for me if I type the 'Print'-key on my keyboard and after this I run my script ...

#include <Clipboard.au3>
#include <ScreenCapture.au3>

_ClipBoard_Open(0) ; open clipboard is associated with the current task
$hBitmap = _ClipBoard_GetData(2) ; $CF_BITMAP
_ScreenCapture_SaveImage(@DesktopDir & '\Screen.bmp', $hBitmap)
_ClipBoard_Close()
Edited by Greenhorn

Share this post


Link to post
Share on other sites

I've been working on something similar. Is there a way to save the clipboard data directly to a .jpg?

Share this post


Link to post
Share on other sites

Greenhorn, thanks. Yours does work ... and after running yours, mine worked. The key is the Open. I was trying the Ex version, which requires a separate Open. Ironically, according to the documentation, the simpler version you used doesn't need the Open:

This function performs all of the steps necessary to get data from the clipboard. It checks to see if the data format is available, opens the clipboard, closes the clipboard and returns the data (converting it to a string if needed. If you need a finer degree of control over retrieving data from the clipboard, you may want to use the _ClipBoard_GetDataEx function.

I'm all for simplicity, and since mine is a one-shot operation, I'll let the function do the open/close.

Is there a way to save the clipboard data directly to a .jpg?

According to the documentation, yes. Just look at _ScreenCapture_SaveImage (but I haven't tried it.)

Share this post


Link to post
Share on other sites

Wow, thanks. I actually read that entry, but only got as far as "Full path and extension of the bitmap file to be saved" before I decided it couldn't help me. I've got to read more thoroughly.

Share this post


Link to post
Share on other sites

Hi orestes72,

you just have to save it as a *.jpg to get your result. Optional you can set the quality of the JPEG.

You've read the wrong function ..., read _ScreenCapture_SaveImage ... :)

#include <Clipboard.au3>
#include <ScreenCapture.au3>

_ClipBoard_Open(0) ; open clipboard is associated with the current task
$hBitmap = _ClipBoard_GetData(2) ; $CF_BITMAP
;_ScreenCapture_SetJPGQuality(75) ; set the value between 0-100
_ScreenCapture_SaveImage(@DesktopDir & '\Screen.jpg', $hBitmap)
_ClipBoard_Close()

Greetz

Share this post


Link to post
Share on other sites

Greenhorn, that was my original implementation. Unfortunately when I use that function, autoit ignores any wpf windows that are on the screen. Drove me crazy!

Thanks for all of your help though! Your first post helped solve a problem that was nagging in the back of my mind for months.

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  
Followers 0