Sign in to follow this  
Followers 0
joelson

how get image from clipboard without loss quality?

5 posts in this topic

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

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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!
¯\_(ツ)_/¯

Share this post


Link to post
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.

get this script
_SaveClip(@ScriptDir & "XXX.jpg")
in this forum

Share this post


Link to post
Share on other sites

thanks UEZ your script is working perfect.

very thanks your help.

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

  • Similar Content

    • SimTheNo1
      By SimTheNo1
      I am busy with building a solution for change monitoring of VOIP call program and to be properly automated means among others need for some limited OCR functionality. Current works of others are way too much overkill for this case what makes the need to build it myself. But to do it properly I significantly have to increase my knowledge about digital graphics management.
      For now I already have discovered here and there some mind blowing  Autoit miracles what can be achieved with Windows own possibilities to manipulate that what is output to the monitor. And as far I can judge there are 2 options to process graphics without use of any external libraries like ImageMagick, FreeImage and so on. These are:
       WinAPI  GDIPlus It is for me quite obvious to have various holes in general understanding of graphics and it is once more very clear what advantages gives consistent general study in 1 or another official institutions like University.  Cause there you are introduced into certain domain of knowledge in a way which usually has been perfected over long period of time. So you are not overloaded by stuff which requires a certain amount of information to be initially clear for you. For example, before starting to solve physics you first learn to read, count and so on and then move to subjects like physics.
      Though in my case opportunity to study in such educational system I had only for 8 years, from my 7th to 15th year of age in the country that was falling apart now Ukraine but used to be USSR, was all what it was. After have immigrated to Netherlands possibilities to study further haven't occur. And this therefor causes often various implications when going deep in that or another field of practical knowledge acquiring for any needed physical result, like programming to perform enormous amount of tasks. In this particular case automating VOIP call program.
      Anyway, right now I think the best direction to move is to concentrate on as basic as possible image management and if someone would maybe explain in general what is a pixel will definitely help. Particularly I am very curious about how to do picture manipulations in Autoit. Especially would help a lot to produce eventually following functions:
      createImage($imageFileName, $width, $height, $color) readImagePixel($imageFileName, $x, $y)  writeImagePixel($imageFileName, $x, $y) I do not know exactly how image is handled in computer but preferably above mentioned functions should deal with so far possible origin of graphics creation on computer. But nevertheless I definitely would love to hear any proposition for solution.
      The problem with explaining screenshots:
       VOIPConnect full Window  Part of Window with control to monitor for changes  Exact location of area where actual changes occur and have to be processed  It comes down to a rectangle of approximately 51 pixel wide and 7 pixel high. In fact if I get to learn as far as to be able exactly read, write and compare 2 images consistently across different computers I could narrow down then even further the area to watch as little as a square of 2-5 pixels wide. 
      To finish here is last detail about particularly no need for ultra fast solution at all. This because it is needed only once when it is first run on a new computer and to have to wait few minutes while it is being set instead of just a few seconds make no sence.
      This is it and what I too think to do beside this very particular case is to purify out beautiful generic Autoit functions for core image manipulation by using WinAPI or/and GDIPlus. 
    • Ian_Mac
      By Ian_Mac
      Func dg_get_images() $x1=0 $y1=0 ;MsgBox(0,"start","start") do $result = _ImageSearch("C:\Users\Server\Desktop\my project\image.png",1,$x1,$y1,0) until $result = 1; if $result=1 Then MouseMove($x1,$y1,3) MouseClick("left", $x1,$y1, 1) WinWait("[Title:TightVNC File Transfer]", "", 10) EndIf  this code works when i search the image, but when i change the location of the desktop icon it can't find the desktop icon anymore.   i wanna try image search area. but i don't know how to use it because i cant understand the parameter for the image search area.  please help me how to solve it thank you in advance.
    • Nareshm
      By Nareshm
      Its possible to past previously copied text to somewhere with autoit script ?
      If yes, How it ?
      example : Before I copied ABCD and then copied the PQRS, how to past the previously copied ABCD ?
    • mmoalem
      By mmoalem
      I seem to have an issue with clipget() - the following bit of code copy URL from chrome address bar - than using clipget() i try to grab the URL into a variable and input it into a spreadsheet. the script did not put anything into the sheet 
      originally I though it was an issue with  OOo/LibO Calc UDF and posted it there :
       
      than I tried writing $NewURL into a text file and than just a msgbox but it comes blank. (the URL is in the clipboard as i can paste it)
       
      send ("!d") ;select URL in browser send ("^c") ;copy selected URL Local $NewURL = ClipGet() MsgBox($MB_ICONINFORMATION + $MB_TOPMOST, $sTitle, $NewURL ) am I doing something wrong? is there an issue with a URL characters?