Jump to content

GDI+ CloneArea problem


Flash
 Share

Recommended Posts

Hi guys,

i got the following code:

#include <GDIPlus.au3>

_GDIPlus_Startup()

$hGUIMinimap = GUICreate("", 400, 200)
$hGUIMinimap1 = GUICtrlCreatePic("", 5, 5, 190, 190)
$hGUIMinimap2 = GUICtrlCreatePic("", 205, 5, 190, 190)

GUISetState()
_FillGraphics()

While 1
    Switch GUIGetMsg()
        Case - 3
            Exit
    EndSwitch
    Sleep(1)
WEnd

Func _FillGraphics()
    $hBitmap = _GDIPlus_ImageLoadFromFile("auge.jpg")
    $hBitmap2 = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, 190, 190)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap2)
    $hNewBitmapHandle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap2)
    _SetBitmapToCtrl($hGUIMinimap1, $hNewBitmapHandle)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _WinAPI_DeleteObject($hNewBitmapHandle)
    
    $hBitmap = _GDIPlus_ImageLoadFromFile("auge.jpg")
    $hBitmap2 = _GDIPlus_BitmapCloneArea($hBitmap, -1, 0, 190, 190)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap2)
    $hNewBitmapHandle = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap2)
    _SetBitmapToCtrl($hGUIMinimap2, $hNewBitmapHandle)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)
    _WinAPI_DeleteObject($hNewBitmapHandle)
EndFunc   ;==>_PrepareBitmap

Func _SetBitmapToCtrl($vCtrlID, $hBitmap)
    Local $hWnd = GUICtrlGetHandle($vCtrlID)
    If $hWnd = 0 Then Return SetError(1, 0, 0)

    Local $vOldStyle = _WinAPI_GetWindowLong($hWnd, -16)
    _WinAPI_SetWindowLong($hWnd, -16, BitOR($vOldStyle, 0xE))

    Local $vOldBmp = _SendMessage($hWnd, 0x0172, 0, $hBitmap)
    If $vOldBmp <> 0 Then _WinAPI_DeleteObject($vOldBmp)
EndFunc   ;==>_SetBitmapToCtrl

Therefore this picture:

Posted Image

This is just an example script that i made.

So i wanna clone an area of 190x190 pixels from a image. the start x and y positions are chosen randomly, and they might get under 0. But with negative startcoordinates the gdi+ function clonearea gives me an error.

i thought about 2 options of myself,

1. Add a black edge around the image, but this wouldnt work with my current code, so i would have to do alot of changes which i wanna avoid.

2. Create a virtual black edge around the image, so the cloning starts at -20 which means, got 20 pixels black then the real image with 170 pixels left.

But i don't even know if this is possible, or maybe there's just a clue to work with another function?

I hope someone is able to help me

greetz

Link to comment
Share on other sites

Have you checked this thread? -> GDI+ Clone

Unfortunately the source code is broken during forum upgrade but have a look also to the german site :D

:D 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

You may be able to work something out by playing around with this example.

;
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled

Global $ApW = 400, $ApH = 200
Global $Button[1]

$hGui = GUICreate("GDIPlus Graphics on Picture Control - OnEvent Mode", $ApW + 40, $ApH + 40)
GUISetOnEvent(-3, "_Quit")
GUISetBkColor(0x303030, $hGui)

$Pic = GUICtrlCreatePic("", 20, 20, 190, 190)
GUICtrlSetBkColor(-1, 0xFFFF00)
GUICtrlSetState(-1, $GUI_DISABLE)
$Pic1 = GUICtrlCreatePic("", 230, 20, 190, 190)
GUICtrlSetBkColor(-1, 0xFFFF00)
GUICtrlSetState(-1, $GUI_DISABLE)

PicSetGraphics($Pic, -160, -40)
PicSetGraphics2($Pic1, 20, 50)

While 1
    Sleep(10)
WEnd

Func PicSetGraphics($cID, $iW, $iH)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local $hWnd, $hBitmap, $hImage, $hGraphic, $hBrush, $hBrush1, $hbmp, $aBmp
    $hWnd = GUICtrlGetHandle($cID)
    _GDIPlus_Startup()
    $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hWnd) ; added
    $hImage = _GDIPlus_BitmapCreateFromGraphics(190, 190, $hGraphicGUI);added
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage); original
    _GDIPlus_GraphicsClear($hGraphic, 0xFF000000) ;added - Instead of bitmap colour, This adds the colour.
    GUISetState(@SW_SHOW, $hGui)

    ;----->  All Graphics Here
    $hBitmap = _GDIPlus_ImageLoadFromFile("auge.jpg")
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $iW, $iH, 640, 512)
    ; -----> End of all Graphics

    $hbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $aBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hbmp)
    _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME))

    ; Save Graphics on picture control
    ;_GDIPlus_ImageSaveToFile($hImage, @DesktopDir & "\TestWrite1.png")
    ;ShellExecute(@DesktopDir & "\TestWrite1.png")

    If $aBmp[0] <> 0 Then _WinAPI_DeleteObject($aBmp[0])
    _WinAPI_DeleteObject($hbmp)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)

    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>PicSetGraphics

Func PicSetGraphics2($cID, $iW, $iH)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $IMAGE_BITMAP = 0
    Local $hWnd, $hBitmap, $hImage, $hGraphic, $hBrush, $hBrush1, $hbmp, $aBmp
    $hWnd = GUICtrlGetHandle($cID)
    _GDIPlus_Startup()
    $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hWnd) ; added
    $hImage = _GDIPlus_BitmapCreateFromGraphics(190, 190, $hGraphicGUI);added
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage); original
    _GDIPlus_GraphicsClear($hGraphic, 0xFF000000) ;added - Instead of bitmap colour, This adds the colour.
    GUISetState(@SW_SHOW, $hGui)

    ;----->  All Graphics Here
    $hBitmap = _GDIPlus_ImageLoadFromFile("auge.jpg")
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, $iW, $iH, 300, 300)
    ; -----> End of all Graphics

    $hbmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $aBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hbmp)
    _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME))

    ; Save Graphics on picture control
    ;_GDIPlus_ImageSaveToFile($hImage, @DesktopDir & "\TestWrite1.png")
    ;ShellExecute(@DesktopDir & "\TestWrite1.png")

    If $aBmp[0] <> 0 Then _WinAPI_DeleteObject($aBmp[0])
    _WinAPI_DeleteObject($hbmp)
    _WinAPI_DeleteObject($hImage)
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)

    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>PicSetGraphics2

Func _Quit()
    Exit
EndFunc   ;==>_Quit
;
Link to comment
Share on other sites

Wow, thanks, with this example i got to my old method but its not flickering anymore!

Here the functions that i use:

$hGUIMap = GUICtrlCreatePic("", 544, 25, 190, 190)
$hWnd = GUICtrlGetHandle($hGUIMinimap)

$hBitmap = _GDIPlus_ImageLoadFromFile("WorldMap.jpg")
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hWnd)
$hImage = _GDIPlus_BitmapCreateFromGraphics(190, 190, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

_GDIPlus_GraphicsClear($hGraphic, 0xFF000000)
_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hBitmap, x, y, 190, 190, 0, 0, 190, 190)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00)
_GDIPlus_GraphicsFillEllipse($hGraphic, 90, 90, 10, 10, $hBrush)
$hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$vCall = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", 0x0172, "int", 0, "int", $hBitmap2)
;_WinAPI_RedrawWindow($hGui, "", "", BitOR(0x0001, 0x0100, 0x0400))

_GDIPlus_BrushDispose($hBrush)
If $vCall[0] <> 0 Then _WinAPI_DeleteObject($vCall[0])
_GDIPlus_GraphicsDispose($hGraphicGUI)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_BitmapDispose($hImage)
_WinAPI_DeleteObject($hBitmap2)
_GDIPlus_BitmapDispose($hBitmap)

Ok, thanks guys =)

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