Jump to content

how get image from clipboard without loss quality?


joelson
 Share

Recommended Posts

get image from clipboard, but _GDIPlus_ImageSaveToFile loss image quality

#include <Clipboard.au3>
#Include <GDIPlus.au3>
_SaveClip(@ScriptDir & "\XXX.jpg")
Func _SaveClip($sFile)
ConsoleWrite($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

please post a sample

thanks any help

Link to comment
Share on other sites

well after you explain why do you save it to JPG "XXX.jpg" then someone probably can make sense of your request for this problem.

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

Try this

;coded by UEZ 2011
#include <clipboard.au3>
#include <gdiplus.au3>
#include <guiconstantsex.au3>
#include <windowsconstants.au3>

$msgb = MsgBox(4, "Clipboard to GUI", "Ready to copy an image from clipboard to GUI?")
If $msgb <> 6 Then Exit

If Not _ClipBoard_Open(0) Then Exit _WinAPI_ShowError ("_ClipBoard_Open failed")
$hMemory = _ClipBoard_GetDataEx($CF_BITMAP)
If $hMemory  = 0 Then Exit _WinAPI_ShowError ("_ClipBoard_GetDataEx failed")

_GDIPlus_Startup()
$hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hMemory)
If $hBitmap = 0 Then Exit _WinAPI_ShowError ("_GDIPlus_BitmapCreateFromHBITMAP failed")

$iWidth = _GDIPlus_ImageGetWidth($hBitmap)
$iHeight = _GDIPlus_ImageGetHeight($hBitmap)

$hGUI = GUICreate("Show Image from Clipboard", $iWidth, $iHeight, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
GUISetState(@SW_SHOW, $hGUI)

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _ClipBoard_Close()
            _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "ImageFromClipboard.png")
            _GDIPlus_BitmapDispose($hBitmap)
            _GDIPlus_GraphicsDispose($hGraphic)
            _GDIPlus_Shutdown()
            GUIDelete($hGUI)
            ExitLoop
    EndSwitch
WEnd
Exit

Image will be save in lossless PNG format!

For the JPG best quality you can use these lines:

Local $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
Local $tParams = _GDIPlus_ParamInit(1)
Local $tData = DllStructCreate("int Quality")
DllStructSetData($tData, "Quality", 100)
Local $pData = DllStructGetPtr($tData)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
Local $pParams = DllStructGetPtr($tParams)
$save = _GDIPlus_ImageSaveToFileEx($hBitmp, @ScriptDir & "ImageFromClipboard.jpg"), $sCLSID, $pParams)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

×
×
  • Create New...