Jump to content

Problem using _GDIPlus_BitmapCloneArea


Recommended Posts

Hello,

I'm having a problem with _GDIPlus_BitmapCloneArea and then using _GDIPlus_GraphicsDrawImage to draw the cloned bmp to my graphics object.

If i load an image from a file and clone that then its working correctly.

If you run this code you'll see i'm getting a black box instead if the cloned image.

Can anyone explain what i'm doing wrong?

#include<GUIConstants.au3>
#include <GDIPlus.au3>

global $gui = GUICreate("BitmapCloneArea test", 500,500,-1,-1)
GUISetState(@SW_SHOW)

_GDIPlus_StartUp()
$hBrush_dark = _GDIPlus_BrushCreateSolid(0xFF996633)
$hBrush_light = _GDIPlus_BrushCreateSolid(0xFFFFFF99)
$Graphics_obj = _GDIPlus_GraphicsCreateFromHWND($gui)

_GDIPlus_GraphicsFillRect($Graphics_obj,0,0,500,250,$hBrush_dark)
_GDIPlus_GraphicsFillRect($Graphics_obj,0,250,500,250,$hBrush_light)

$bitmap_obj = _GDIPlus_BitmapCreateFromGraphics(500, 500, $Graphics_obj) ; Creates a Bitmap object based on a Graphics object, a width, and a height
$bitmap_obj_clone = _GDIPlus_BitmapCloneArea($bitmap_obj,50,125,50,250,$GDIP_PXF32PARGB) ; Create a clone of a Bitmap object from the coordinates and format specified
$bitmap_handle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($bitmap_obj) ; Create a handle to a bitmap from a bitmap object
$bitmap_obj2 = _GDIPlus_BitmapCreateFromHBITMAP($bitmap_handle) ; Create a Bitmap object from a bitmap handle
_GDIPlus_ImageSaveToFile($bitmap_obj2, "testfile.bmp")
_GDIPlus_GraphicsDrawImage($Graphics_obj, $bitmap_obj2, 100, 200)

While 1 
    local $Msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    sleep(20)
WEnd
Link to comment
Share on other sites

Try this:

#include<GUIConstants.au3>
#include <GDIPlus.au3>

global $gui = GUICreate("BitmapCloneArea test", 500,500,-1,-1)
GUISetState(@SW_SHOW)

_GDIPlus_StartUp()
$hBrush_dark = _GDIPlus_BrushCreateSolid(0xFF996633)
$hBrush_light = _GDIPlus_BrushCreateSolid(0xFFFFFF99)
$Graphics_obj = _GDIPlus_GraphicsCreateFromHWND($gui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(500, 500, $Graphics_obj)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

_GDIPlus_GraphicsFillRect($hBackbuffer,0,0,500,250,$hBrush_dark)
_GDIPlus_GraphicsFillRect($hBackbuffer,0,250,500,250,$hBrush_light)
_GDIPlus_GraphicsDrawImage($Graphics_obj, $hBitmap, 0, 0)

$bitmap_obj_clone = _GDIPlus_BitmapCloneArea($hBitmap,50,125,50,250,$GDIP_PXF32RGB) ; Create a clone of a Bitmap object from the coordinates and format specified
;~ _GDIPlus_GraphicsDrawImage($Graphics_obj, $bitmap_obj_clone, 0, 0)
_GDIPlus_ImageSaveToFile($bitmap_obj_clone, @ScriptDir & "\testfile.bmp")

While 1
    local $Msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        _GDIPlus_BrushDispose($hBrush_dark)
        _GDIPlus_BrushDispose($hBrush_light)
        _GDIPlus_BitmapDispose($hBitmap)
        _GDIPlus_BitmapDispose($bitmap_obj_clone)
        _GDIPlus_GraphicsDispose($hBackbuffer)
        _GDIPlus_GraphicsDispose($Graphics_obj)
        _GDIPlus_Shutdown()
        Exit
    EndIf
WEnd

Br,

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

Thanks alot UEZ,

It works now. So you create a buffer en then draw on it.

When you're done you draw the buffered image to the graphics object.

but I don't really understand why the problem is solved by using $backbuffer

Can you explain in short what exactly is happening, why your code does not give a black image after using _GDIPlus_BitmapCloneArea

Link to comment
Share on other sites

The important lines are:

$Graphics_obj = _GDIPlus_GraphicsCreateFromHWND($gui)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(500, 500, $Graphics_obj)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

You can only draw directly with the default functions to a graphic handle but you cannot save a graphic handle to a bitmap directly.

That is the reason why the $hBitmap and the $hBackbuffer is created. The _GDIPlus_ImageGetGraphicsContext() function will allow you to draw to the bitmap directly using the default function. Thus you can save the bitmap afterwards (don't mix it up with HBITMAPS (GDI)!

To display the bitmap it needs to be copied to the graphic handle -> _GDIPlus_GraphicsDrawImage()

2nd benefit is when drawing animations it will not flicker.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...