Jump to content

GDIPlus_MatrixRotate Example


Malkey
 Share

Recommended Posts

While searching for an example to show how GDIPlus_MatrixRotate is used, I came across a C function similar to the one shown here.

It is nothing great, but I got it to work.

The script captures the whole screen. Then captures the top left corner of the screen and places duplicates of the top corner at various rotated angles on the image of the whole screen, Then saves to MyDocumentsDir as GDIPlus_Image.jpg.

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

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hBitmap1, $hBitmap2, $hImage1, $hImage2, $hGraphic, $width , $height

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

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

    ; Capture screen region
    $hBitmap2 = _ScreenCapture_Capture ("", 0, 0, 400, 300) 
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)
    
    $width = _GDIPlus_ImageGetWidth ($hImage2)
    $height = _GDIPlus_ImageGetHeight ($hImage2)
    
    ; Draw one image in another
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)
    
    ;DrawInsert($hGraphic, $hImage2, $iX, $iY, $nAngle,    $iWidth,    $iHeight, $iARGB = 0xFF000000, $nWidth = 1)
     DrawInsert($hGraphic, $hImage2, 350, 100,       0, $width + 2, $height + 2,          0xFFFF8000,           2)
     DrawInsert($hGraphic, $hImage2, 340,  50,      15,        200,         150,          0xFFFF8000,           4)
     DrawInsert($hGraphic, $hImage2, 310,  30,      35, $width + 4,  $height + 4,         0xFFFF00FF,           4)
     DrawInsert($hGraphic, $hImage2, 320, 790,     -35,    $width ,      $height  )

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

    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage1)
    _GDIPlus_ImageDispose ($hImage2)
    _WinAPI_DeleteObject ($hBitmap1)
    _WinAPI_DeleteObject ($hBitmap2)
    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc   ;==>_Main

; #FUNCTION# ==================================================================================================
 ;Name...........: DrawInsert
; Description ...: Draw one image in another
; Syntax.........: DrawInsert($hGraphic, $hImage2, $iX, $iY, $nAngle, $iWidth, $iHeight, $iARGB = 0xFF000000, $nWidth = 1)
; inserts Graphics $hImage2 into $hGraphic
; Parameters ....: $hGraphics   - Handle to a Graphics object
;                  $hImage      - Handle to an Image object to be inserted
;                  $iX          - The X coordinate of the upper left corner of the inserted image
;                  $iY          - The Y coordinate of the upper left corner of the inserted image
;                  $iWidth      - The width of the rectangle Border around insert
;                  $iHeight     - The height of the rectangle Border around insert
;                  $iARGB       - Alpha, Red, Green and Blue components of pen color - Border colour
;                  $nWidth      - The width of the pen measured in the units specified in the $iUnit parameter - Border Width

; Return values .: Success      - True
;                  Failure      - False
;==================================================================================================
Func DrawInsert($hGraphic, $hImage2, $iX, $iY, $nAngle, $iWidth, $iHeight, $iARGB = 0xFF000000, $nWidth = 1)
    dim $hMatrix, $hPen2
    
    ;Rotation Matrix
    $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixRotate($hMatrix, $nAngle, "False")   
    _GDIPlus_GraphicsSetTransform($hGraphic , $hMatrix) 
    
    _GDIPlus_GraphicsDrawImage ($hGraphic, $hImage2, $iX, $iY)      
    
     ;get pen + color
    $hPen2 = _GDIPlus_PenCreate($iARGB , $nWidth)   

    ; Draw a frame around the inserted image
    _GDIPlus_GraphicsDrawRect ($hGraphic, $iX, $iY,  $iWidth, $iHeight , $hPen2)
    
    ; Clean up resources
    _GDIPlus_MatrixDispose($hMatrix)
    _GDIPlus_PenDispose ($hPen2)
    Return 1
EndFunc
Link to comment
Share on other sites

I noticed there were many gdi+ functions missing from the include, so i wrote my own. so the answer to my above post is:

Func _GDIPlus_CreateBitmap($Width, $Height, $Format)
    Local $aResult
    
    $aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromScan0", "int", $Width, "int", $Height, "int", 0, "int", $Format, "hwnd", 0, "int*", 0)
    Return SetError($aResult[0], 0, $aResult[6])
EndFunc;==>_GDIPlus_CreateBitmap

and can be used:

$hGraphic = _GDIPlus_CreateBitmap(300, 300, 0x26200A)

*untested (i havent used autoit much so am not sure whether this is all correct)

Off topic: Where do i go in the forum to view topics i have responded to?

Edited by tic
Link to comment
Share on other sites

Very nice indeed.

Mind if I add it to the help as the example for _GDIPlus_MatrixRotate?

The example looks like it could also be an example for a few other GDI+ functions.

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

I noticed there were many gdi+ functions missing from the include, so i wrote my own. so the answer to my above post is:

Off topic: Where do i go in the forum to view topics i have responded to?

tic - The short answer to your first post is, " I don't know". I am happy you found closure to your question.

I noticed there were a few existing gdi+ functions missing examples from the Help file. _MatrixRotate got my attention.

No example, link to msdn not working at the time, search on Autoit forums - nothing, I did get a little obsessive.

When I read your first post I thought you might find another way of using GDI+_MatrixRotate. From your second post, it appears you have the ability to follow any track your interests take you.

Off topic: Above the avatar at left of post is the member's name. Click on name. click on Find Member's Posts. That is how I find my own posts. Probably an easier way.

Link to comment
Share on other sites

Mind if I add it to the help as the example for _GDIPlus_MatrixRotate?

The example looks like it could also be an example for a few other GDI+ functions.

I don't mind. Glad I could contribute. Any example is a good example. Feel free to do with it whatever you like.
Link to comment
Share on other sites

The $iX and $iY values in the first example of _GDIPlus_MatrixRotate() were found by trial and error.

They are the starting coordinates before rotation. It is better to enter the coordinates of the object after the object has been rotated. This is what is seen.

This can be done by rotation of the coordinate axes. The formulae being:-

$nXt = $nX*cos($nAngle*$nPI/180) + $nY*sin($nAngle*$nPI/180) ; $nXt - X coordinate before translation

$nYt = -$nX*sin($nAngle*$nPI/180) + $nY*cos($nAngle*$nPI/180) ; $nYt - Y coordinate before translation

;
#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>

Global Const $nPI = 3.1415926535897932384626433832795
Global $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $hBitmap1, $hImage, $hClone, $iX, $iY, $hBitmap
Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hMatrix, $width, $height, $hBitmap1, $hImage1

   ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    GUISetBkColor(0xEAFEFE)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()

   ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)


   ;DrawGraphic($hGraphic, $nX, $nY, $nAngle,   $iWidth,    $iHeight, $iARGB = 0xFF000000)
   ;Cross
    DrawGraphic($hGraphic, 120, 150, 45, 100, 20, 0xFFFF8000)
    DrawGraphic($hGraphic, 120, 150, 135, 100, 20, 0xFF00FF00)
    DrawGraphic($hGraphic, 120, 150, 225, 100, 20, 0xFF0505FB)
    DrawGraphic($hGraphic, 120, 150, 315, 100, 20, 0xFFFF00FF)

   ;Square
    DrawGraphic($hGraphic, 260, 200, 0, 100, 20, 0xFFFF8000)
    DrawGraphic($hGraphic, 260, 100, 90, 100, 20, 0xFF00FF00)
    DrawGraphic($hGraphic, 360, 100, 180, 100, 20, 0xFF0505FB)
    DrawGraphic($hGraphic, 360, 200, 270, 100, 20, 0xFFFF00FF)

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

   ; Clean up resources
    _GDIPlus_ImageDispose($hClone)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBitmap1)

   ;_GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)

   ; Shut down GDI+ library
    _GDIPlus_Shutdown()

EndFunc  ;==>_Main

; #FUNCTION# ==================================================================================
; Name...........: DrawGraphic
; Description ...: Draw Graphics object at rotated angle
; Syntax.........: DrawGraphic($hGraphic, $iX, $iY, $nAngle, $iWidth, $iHeight, $iARGB = 0xFF000000)
; inserts Graphics $hImage2 into $hGraphic
; Parameters ....: $hGraphics   - Handle to a Graphics object
;                 $nX         - The X coordinate of the upper left corner of the rotated Graphics object
;                 $nY         - The Y coordinate of the upper left corner of the rotated Graphics object
;                 $iWidth     - The width of the rectangle of the Graphics object
;                 $iHeight   - The height of the rectangle of the Graphics object
;                 $iARGB       - Alpha, Red, Green and Blue components of pen color - Border colour

; Return value .:             - True
;
;===========================================================================================
Func DrawGraphic($hGraphic, $nX, $nY, $nAngle, $nWidth, $nHeight, $iARGB = 0xFF000000)
    Local $hMatrix, $nXt, $nYt
   ; $nXt    - The X coordinate of the upper left corner of the Graphics object before rotation
   ; $nYt    - The Y coordinate of the upper left corner of the Graphics object before rotation

   ;Rotation Matrix
    $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixRotate($hMatrix, $nAngle, "False")
    _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)

   ;Rotation of Coordinate Axes formulae
    $nXt = $nX * Cos($nAngle * $nPI / 180) + $nY * Sin($nAngle * $nPI / 180); $nXt - X coordinate before translation
    $nYt = -$nX * Sin($nAngle * $nPI / 180) + $nY * Cos($nAngle * $nPI / 180); $nYt - Y coordinate before translation

    $hBrush = _GDIPlus_BrushCreateSolid($iARGB)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 12, 1)
    $tLayout = _GDIPlus_RectFCreate($nXt, $nYt, $nWidth, $nHeight)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrush)
    Sleep(1000)

   ; Clean up resources
    _GDIPlus_MatrixDispose($hMatrix)

    Return 1
EndFunc  ;==>DrawGraphic
[left];

In the first post the return of the function is not True or False. I have Return 1

Edit: Changed to CODE box

Edited by Malkey
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...