Jump to content
Sign in to follow this  

copy a chunk of an image

Recommended Posts

in the help is something similar to what i want to do but instead of screen capture i want to cut a rectangular chunk out of an image, _GDIPlus_GraphicsDrawImage() let me dwaw the image where i want on the x y coordinates but i want to copy a certain Width and Height from a custom X and Y coordinate.

i went to look at ScreenCapture.au3 seems that _WinAPI_BitBlt does the job but i don't understand it, is there a simple way staying with _GDIPlus_Graphics? or any tips on how to do it with _WinAPI_BitBlt ?

help file example

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


Func Example()
    Local $hBitmap1, $hBitmap2, $hImage1, $hImage2, $hGraphic

    ; Initialize GDI+ library

    ; Capture full screen
    $hBitmap1 = _ScreenCapture_Capture("")
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap1)

    ; Capture screen region
    $hBitmap2 = _ScreenCapture_Capture("", 0, 0, 400, 300)
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)

    ; Draw one image in another
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2, 100, 100)

    ; Draw a frame around the inserted image
    _GDIPlus_GraphicsDrawRect($hGraphic, 100, 100, 400, 300)

    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage1, @MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Clean up resources

    ; Shut down GDI+ library

    ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.jpg")
EndFunc   ;==>Example


Edited by jvds

Share this post

Link to post
Share on other sites

You can use _GDIPlus_GraphicsDrawImageRectRect or _GDIPlus_BitmapCloneArea for example.

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

thank you i went past _GDIPlus_GraphicsDrawImageRectRect several times and didn't notice :sweating:

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  

  • Similar Content

    • By rootx
      #include <GDIPlus.au3> #include <GuiConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() Local $hImage, $sFile, $hGUI, $hGraphic, $hThumbnail, $iW_new, $iH_new $sFile = FileOpenDialog("Please select an image", "", "Image (*.jpg;*.png;*.bmp;*.gif;*.tif)", BitOR($FD_PATHMUSTEXIST, $FD_FILEMUSTEXIST)) If @error Then Exit MsgBox(BitOR($MB_TOPMOST, $MB_ICONERROR), "Error", "No image file has been selected", 30) _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($sFile) If @error Or Not $hImage Then MsgBox(BitOR($MB_TOPMOST, $MB_ICONERROR), "Error", "This file isn't supported by GDIPlus!") Else $hGUI = GUICreate("GDI+ _GDIPlus_ImageGetThumbnail Demo", 320, 200) GUISetBkColor(0x202020) GUISetState() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) $hThumbnail = _GDIPlus_ImageGetThumbnail($hImage, 96, 96) $iW_new = _GDIPlus_ImageGetWidth($hThumbnail) $iH_new = _GDIPlus_ImageGetHeight($hThumbnail) _GDIPlus_GraphicsDrawImageRect($hGraphic, $hThumbnail, (320 - $iW_new) / 2, (200 - $iH_new) / 2, $iW_new, $iH_new) ;center image in GUI Do Until GUIGetMsg() = $GUI_EVENT_CLOSE _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hThumbnail) EndIf _GDIPlus_Shutdown() EndFunc ;==>Example The image is resized but also rotated, why? I just want to reduce its size without changing the appearance.
      Someone can give me some information.
    • By JonBMN
      I'm trying to put an icon in the top of the GUI window using _GDIPlus.au3. I've tried using the Sleep(20) that has been suggested to no avail. Thank you for any and all help.
      Func GUI() Local $hImageGreen, $hImageRed, $hGraphic, $CheckRunning, $Parent, $ButtonInstall, $buttonUninstall, $ButtonStart, $ButtonStop, $ButtonConfigure _GDIPlus_Startup() $hImageRed = _GDIPlus_ImageLoadFromFile(@UserProfileDir & "\Desktop\SRRS\console\Red.png") $hImageGreen = _GDIPlus_ImageLoadFromFile(@UserProfileDir & "\Desktop\SRRS\console\Green.png") $Parent = GUICreate("Console", 345, 225) ConsoleWrite($hImageRed & "<-red green->" & $hImageGreen) $CheckRunning = ProcessExists("test.exe") If $CheckRunning <> 0 Then GUICtrlCreateLabel("its running!", 110, 40) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Parent) Sleep(20) _GDIPlus_GraphicsDrawImage($hGraphic, $hImageGreen, 110, 40) Else GUICtrlCreateLabel("not running!", 108, 40) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Parent) Sleep(20) _GDIPlus_GraphicsDrawImage($hGraphic, $hImageRed, 110, 40) EndIf $ButtonInstall = GUICtrlCreateButton("Install", 55, 85, 70) $ButtonUninstall = GUICtrlCreateButton("Uninstall", 55, 125, 70) $ButtonStart = GUICtrlCreateButton("Start", 210, 85) $ButtonStop = GUICtrlCreateButton("Stop", 210, 125) $ButtonConfigure = GUICtrlCreateButton("Configure", 131, 165, 70) GUICtrlCreateLabel("Is it Running?", 110, 10) GUISetState(@SW_SHOW) ;shows the GUI window
    • By Artisan
      I'm coding a GUI that using a lot of GDI (not GDI+) output.  I've been manually redrawing things as needed via WM_PAINT.  It's awkward and slow, and it can flicker a lot.  Then I found out about _WinAPI_BitBlt.  I've looked all over the forums here, and all over the internet in general.  I think I understand the basics of it, but I can't get it to work.  I think what I want to do is manipulate $hDCSource and then BitBlt it onto $hDCDest.  Here's my reducer sample:
      #include <GUIConstantsEx.au3> #include <WinAPI.au3> #include <WindowsConstants.au3> Global Const $Width = 100, $Height = 100 Global $Left = 50, $Top = 50 Global $hwnd, $hDCDest, $hDCSource, $hPenLil, $hPenFat, $hBrush Main() Func Main() ; GUI & GDI handles $hwnd = GUICreate("BitBlt Test", 300, 300) $hDCDest = _WinAPI_GetDC($hwnd) $hDCSource = _WinAPI_CreateCompatibleDC($hDCDest) $hPenLil = _WinAPI_CreatePen($PS_SOLID, 1, 0xFFFFFF) $hPenFat = _WinAPI_CreatePen($PS_SOLID, 3, 0x00FF00) $hBrush = _WinAPI_CreateSolidBrush(0xFFFFFF) ; Fill white rectangle Local $tRect = DllStructCreate($tagRect) DllStructSetData($tRect, "Left", $Left) DllStructSetData($tRect, "Top", $Top) DllStructSetData($tRect, "Right", $Left + $Width) DllStructSetData($tRect, "Bottom", $Top + $Height) _WinAPI_FillRect($hDCSource, $tRect, $hBrush) ; Thick green outline _WinAPI_SelectObject($hDCSource, $hPenFat) _WinAPI_MoveTo($hDCSource, $Left, $Top) _WinAPI_LineTo($hDCSource, $Left + $Width, $Top) _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height) _WinAPI_LineTo($hDCSource, $Left, $Top + $Height) _WinAPI_LineTo($hDCSource, $Left, $Top) ; Thin black outline _WinAPI_SelectObject($hDCSource, $hPenLil) _WinAPI_MoveTo($hDCSource, $Left, $Top) _WinAPI_LineTo($hDCSource, $Left + $Width, $Top) _WinAPI_LineTo($hDCSource, $Left + $Width, $Top + $Height) _WinAPI_LineTo($hDCSource, $Left, $Top + $Height) _WinAPI_LineTo($hDCSource, $Left, $Top) GUIRegisterMsg($WM_PAINT, "WM_PAINT") GUISetState(@SW_SHOW, $hwnd) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Quit() EndSwitch WEnd EndFunc Func WM_PAINT() ; _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $BLACKNESS) ; Works _WinAPI_BitBlt($hDCDest, $Left, $Top, $Width, $Height, $hDCSource, $Left, $Top, $SRCCOPY) ; Doesn't work EndFunc Func Quit() _WinAPI_ReleaseDC($hwnd, $hDCDest) _WinAPI_DeleteDC($hDCSource) _WinAPI_DeleteObject($hBrush) _WinAPI_DeleteObject($hPenLil) _WinAPI_DeleteObject($hPenFat) GUIDelete($hwnd) Exit EndFunc What am I doing wrong?
  • Create New...