Jump to content

Save clipboard to file


notsure
 Share

Recommended Posts

Hello,

I'm trying to save an Image to a file from the clipboard. This might sound a little odd but the only way to get the image is by copying it to the clipboard and then i want autoit to save it to a JPG file. Is there any way to achieve this?

In the helpfile the only feature which comes close to what i want is "_ClipPut" but thats only to put files from the harddisk to the clipboard. I want it the other way around, so i save a file to the HDD from the clipboard.

Any suggestions? ;) Would be appreciated.

Edit:

I'm trying to do it this way:

$hImage = _ClipBoard_GetData($CF_BITMAP)

  ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Capture full screen

    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hImage)
    _GDIPlus_ImageSaveToFile ($hImage1, "C:\GDIPlus_Image.jpg")

     ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

But it isn't working, I don't get a file at all.

Edited by notsure
Link to comment
Share on other sites

Hello,

I'm trying to save an Image to a file from the clipboard. This might sound a little odd but the only way to get the image is by copying it to the clipboard and then i want autoit to save it to a JPG file. Is there any way to achieve this?

In the helpfile the only feature which comes close to what i want is "_ClipPut" but thats only to put files from the harddisk to the clipboard. I want it the other way around, so i save a file to the HDD from the clipboard.

Any suggestions? ;) Would be appreciated.

Edit:

I'm trying to do it this way:

$hImage = _ClipBoard_GetData($CF_BITMAP)

  ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Capture full screen

    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hImage)
    _GDIPlus_ImageSaveToFile ($hImage1, "C:\GDIPlus_Image.jpg")

     ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

But it isn't working, I don't get a file at all.

Why not just use:

_ScreenCapture_Capture ()

This will take a screenshot and save it to a jpg..look for it in the help file.

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

Interesting question ;)....

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

If Not _ClipBoard_Open(WinGetHandle(AutoItWinGetTitle())) Then MsgBox(0, "", "_ClipBoard_Open failed")
$hClipboardImage = _ClipBoard_GetDataEx($CF_BITMAP)
_ClipBoard_Close()

_GDIPlus_Startup()
$hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hClipboardImage)
$sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
_GDIPlus_ImageSaveToFileEx($hBitmap, @ScriptDir & "\" & TimerInit() & "_Clipboard_Image.jpg", $sCLSID, 0)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
Link to comment
Share on other sites

You're welcome ;), here's a little check added:

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

If _ClipBoard_IsFormatAvailable($CF_BITMAP) Then
    ConsoleWrite("+Bitmap found on Clipboard" & @CRLF)

    If Not _ClipBoard_Open(0) Then
        MsgBox(16, "Error", "_ClipBoard_Open failed")
        Exit
    EndIf
    $hClipboardImage = _ClipBoard_GetDataEx($CF_BITMAP)
    _ClipBoard_Close()

    _GDIPlus_Startup()
    $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hClipboardImage)
    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
    _GDIPlus_ImageSaveToFileEx($hBitmap, @ScriptDir & "\" & TimerInit() & "_Clipboard_Image.jpg", $sCLSID, 0)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndIf
Edited by KaFu
Link to comment
Share on other sites

  • 3 weeks later...

Here is another way to do this:

First you capture the window, this will work on hidden windows, but you will loose transparency:

http://www.autoitscript.com/forum/index.php?showtopic=108822&view=findpost&p=834359

Func _CaptureWindow($hWnd)
    Local $WM_PAINT = 0x000F
    Local $WM_PRINT = 0x317
    Local $PRF_CHILDREN = 0x10; Draw all visible child windows.
    Local $PRF_CLIENT = 0x4 ; Draw the window's client area.
    Local $PRF_OWNED = 0x20 ; Draw all owned windows.
    Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.
    Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window

    Local $pos = WinGetPos($hWnd)
    Local $Width = $pos[2]
    Local $Height = $pos[3]

    Local $hDC = _WinAPI_GetDC($hWnd)
    Local $memDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)

    _WinAPI_SelectObject ($memDC, $memBmp)

    Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)
    $Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))

    Local $hBMP=_GDIPlus_BitmapCreateFromHBITMAP($memBmp)
    Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)

    _WinAPI_DeleteObject($hDC)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _WinAPI_DeleteDC($memDC)
    _WinAPI_DeleteObject ($memBmp)
    _WinAPI_DeleteDC($hDC)

    Return $hHBITMAP
EndFunc ;==>_CaptureWindow()

Then you can use these functions to apply transparency back to the image:

http://www.autoitscript.com/forum/index.php?showtopic=106797&view=findpost&p=753680

Local $hBITMAP=_GDIPlus_BitmapCreateFromHBITMAP(_CaptureWindow(WinGetHandle('YOUR WINDOW'))) ;looses transparency
    Local $hBMP=_ImageColorToTransparent($hBitmap,0xABCDEF)
    Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)

The capturewindow code could be modified to return a bitmap rather than an hbitmap since it is a double conversion - but my script needs the hbitmap multiple times and the bmp only once.

The original solution doesn't work in some instances on Win7 x64 - at least for my script. My solution does work on that OS.

Edited by YellowLab

You can't see a rainbow without first experiencing the rain.

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