Jump to content

_GDIPlus Question


Recommended Posts

Hello guys,

can someone please help me with this one? What's wrong with the code below? The Problem is that the $hImage is saved correctly, but the second is not! (?)

I tried to add an additional "$hImg2 = _GDIPlus_BitmapCreateFromHBITMAP ($hImage2)" and tried to save that aswell, but both are not saved. - The output is just one file, the GDIPlus_Image.bmp.

The script is supposed to take a screenshot, internally create a copy of it in grayscale, and then saving both to bmp files.

My second question is concerning the clean up - which of my handles need _GDIPlus_ImageDispose and which ones need _WinAPI_DeleteObject ?

I'm pretty confused with the HBITMAP and Bitmap thingies.^^

; Capture full screen
    $hBMP = _ScreenCapture_Capture ("", 0, 0, -1, -1, False); with "" -> needs _WinAPI_DeleteObject($hBMP)

; Initialize GDI+ library
    _GDIPlus_Startup ()

; Draw bitmap to GUI
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    
    $hImage2 = _GDIPlus_BitmapCloneArea($hImage, 0, 0, @DesktopWidth, @DesktopHeight, $GDIP_PXF16GRAYSCALE)
    $hImg2 = _GDIPlus_BitmapCreateFromHBITMAP ($hImage2)
    
               ; Save resultant image
                _GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\GDIPlus_Image.bmp")
    _GDIPlus_ImageSaveToFile ($hImage2, @MyDocumentsDir & "\GDIPlus_Image_gray1.bmp")
    _GDIPlus_ImageSaveToFile ($hImg2, @MyDocumentsDir & "\GDIPlus_Image_gray2.bmp")

; Clean up resources
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_ImageDispose ($hImage2)
    _GDIPlus_ImageDispose ($hImg2)
    _WinAPI_DeleteObject ($hBMP)

; Shut down GDI+ library
    _GDIPlus_ShutDown ()
You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time. Abraham Lincoln - http://www.ae911truth.org/ - http://www.freedocumentaries.org/
Link to comment
Share on other sites

It seems that the $GDIP_PXF16GRAYSCALE parameter is to blame. This works fine when the $GDIP_PXF16GRAYSCALE is changed to another value, such as $GDIP_PXF16RGB555. I guess the GDI fails when converting the image type that you have captured to a grayscale format.

This is not the first time someone has has a problem converting something to grayscale:

http://www.autoitscript.com/forum/index.ph..._PXF16GRAYSCALE

Sorry I can't help, but this is either a bug in the AutoIt GDIPlus Library or a limitation of GDIPlus.

Also, look at the code I used for a lesson in debugging:

#include <GDIPlus.au3>
#include <ScreenCapture.au3>

; Capture full screen
    _ScreenCapture_SetBMPFormat(1)

    $hBMP = _ScreenCapture_Capture ("", 0, 0, -1, -1, False); with "" -> needs _WinAPI_DeleteObject($hBMP)

; Initialize GDI+ library
    _GDIPlus_Startup ()

; Draw bitmap to GUI
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
    _Tracelog(">The following value being equal to zero indicates failure:"&@LF& _
                ">$hImage="& $hImage)
    
    Local $iX, $iY
    
    $iX = _GDIPlus_ImageGetWidth($hImage)
    
    $iY = _GDIPlus_ImageGetHeight ($hImage)
    
    _TraceLog("-> Testing To Make Sure That @DesktopWidth and @DesktopHeight are the actual height and width of the image:"& @LF & _
                "-> $iX="&$iX&@LF& _
                "-> $iY="&$iY&@LF& _
                "-> @DesktopWidth="&@DesktopWidth&@LF& _
                "-> @DesktopHeight="&@DesktopHeight)
    
    $hImage2 = _GDIPlus_BitmapCloneArea($hImage, 0, 0, @DesktopWidth, @DesktopHeight, $GDIP_PXF16GRAYSCALE);Change to $GDIP_PXF16RGB555 and it will work
    _Tracelog(">The following value being equal to zero indicates failure:"&@LF& _
                ">$hImage2="& $hImage2)
    
; Save resultant image
    _GDIPlus_ImageSaveToFile ($hImage, @MyDocumentsDir & "\GDIPlus_Image.bmp")
    _GDIPlus_ImageSaveToFile ($hImage2, @MyDocumentsDir & "\GDIPlus_Image_gray1.bmp")

; Clean up resources
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_ImageDispose ($hImage2)
    _WinAPI_DeleteObject ($hBMP)

; Shut down GDI+ library
    _GDIPlus_ShutDown ()
    
Func _Tracelog($sText)
    ConsoleWrite($sText&@LF)
EndFunc

It also appears that you are cleaning up the resources correctly. Yes, you do use _WinAPI_DeleteObject () to delete the screenshot object.

- The Kandie Man ;-)

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

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