Jump to content

GDI+ Rotate total begginer


Recommended Posts

Hello Everyone..

I have searched the forum and found many examples for rotating an Image.

I tried to incorporate the same into my script but wasn't able to do so successfully.

I guess my lack of concepts for this area was the reason.

In the following code the Image isn't rotate by 180 degrees. I'm missing something..!

Please could anybody help me find out the problems in my code

#include <APIConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

OnAutoItExitRegister("OnAutoItExit")
_GDIPlus_Startup()

$Form1 = GUICreate("Form1", 593, 453, 193, 115)
GUICtrlCreateEdit("Hello There", 10, 10)
GUICtrlCreateButton("Get ctl image", 224, 176, 140, 30, 0)

GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case 4
GetControlBitmap(GUICtrlGetHandle(3), 180)
EndSwitch
WEnd

Func GetControlBitmap($hWnd, $iRotate = 0)


$Width = _WinAPI_GetWindowWidth($hWnd)
$Height = _WinAPI_GetWindowHeight($hWnd)

$hDC = _WinAPI_GetDC($hWnd)
$memDC = _WinAPI_CreateCompatibleDC($hDC)
$memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject($memDC, $memBmp)

; use _WinAPI_BitBlt for copying
_WinAPI_BitBlt($memDC, 0, 0, $Width, $Height, $hDC, 0, 0, $SRCCOPY)


$hImage = _GDIPlus_BitmapCreateFromHBITMAP($memBmp)
If $iRotate Then
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)

$hMatrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixRotate($hMatrix, $iRotate, False)

_GDIPlus_GraphicsSetTransform($hGraphics, $hMatrix)

;Again redraw the image so that it turns to 180degree. <<<<<<<<<<<<<<<<<<<<<<< dont know why it doesnt work
_GDIPlus_GraphicsDrawImage($hGraphics, $hImage, 0, 0)

;Lets save and find out what happens
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & '\ctl.bmp') ;.BMP
ShellExecute(@ScriptDir & '\ctl.bmp')

_GDIPlus_MatrixDispose($hMatrix)
_GDIPlus_GraphicsDispose($hGraphics)
EndIf

_WinAPI_ReleaseDC($hWnd, $hDC)
_WinAPI_DeleteDC($memDC)
_WinAPI_DeleteObject($memBmp)

Return $hImage

EndFunc ;==>GetControlBitmap

Func OnAutoItExit()

_GDIPlus_Shutdown()

EndFunc ;==>OnAutoItExit

Thanks for your time :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

You forgot to translate the matrix! - > _GDIPlus_MatrixTranslate($hMatrix, $Width / 2, $Height / 2)

Sometging like this here:

...
If $iRotate Then
        $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
        DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "handle", $hGraphics, "int", 5)
        $hMatrix = _GDIPlus_MatrixCreate()
        _GDIPlus_MatrixTranslate($hMatrix, $Width / 2, $Height / 2)
        _GDIPlus_MatrixRotate($hMatrix, $iRotate, False)
...

If you want to rotate only in 90° steps then have a look to

; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_ImageRotateFlip
; Description ...: Rotates and flips an image
; Syntax.........: _GDIPlus_ImageRotateFlip($hImage, $iRotateFlipType)
; Parameters ....: $hImage          - Pointer to an Image object
;                  $iRotateFlipType - Type of rotation and flip:
;                  |0 - No rotation and no flipping (A 180-degree rotation, a horizontal flip and then a vertical flip)
;                  |1 - A 90-degree rotation without flipping (A 270-degree rotation, a horizontal flip and then a vertical flip)
;                  |2 - A 180-degree rotation without flipping (No rotation, a horizontal flip folow by a vertical flip)
;                  |3 - A 270-degree rotation without flipping (A 90-degree rotation, a horizontal flip and then a vertical flip)
;                  |4 - No rotation and a horizontal flip (A 180-degree rotation followed by a vertical flip)
;                  |5 - A 90-degree rotation followed by a horizontal flip (A 270-degree rotation followed by a vertical flip)
;                  |6 - A 180-degree rotation followed by a horizontal flip (No rotation and a vertical flip)
;                  |7 - A 270-degree rotation followed by a horizontal flip (A 90-degree rotation followed by a vertical flip)
; Return values .: Success      - True
;                  Failure      - False and either:
;                  |@error and @extended are set if DllCall failed
;                  |$GDIP_STATUS contains a non zero value specifying the error code
; Remarks .......: None
; Related .......: None
; Link ..........; @@MsdnLink@@ GdipImageRotateFlip
; Example .......; No
; ===============================================================================================================================
Func _GDIPlus_ImageRotateFlip($hImage, $iRotateFlipType)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipImageRotateFlip", "handle", $hImage, "int", $iRotateFlipType)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageRotateFlip

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

You forgot to translate the matrix! - > _GDIPlus_MatrixTranslate($hMatrix, $Width / 2, $Height / 2)

Didn't knew what it does.

So, now I understand that with my script the Image's Origin being the top left corner, it went out of the image

Do I understand it right ? And what does the GdipSetInterpolationMode does ?

Thanks for the Help UEZ

Thank you so much :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Yes, the origin is always 0, 0.

Indeed, forgot to remove the line because it is not needed I think.

Here the description:

; #FUNCTION# ====================================================================================================================
; Name...........: _GDIPlus_GraphicsSetInterpolationMode
; Description ...: Sets the interpolation mode of a Graphics object
; Syntax.........: _GDIPlus_GraphicsSetInterpolationMode($hGraphics, $iInterpolationMode)
; Parameters ....: $hGraphics            - Pointer to a Graphics object
;                  $iInterpolationMode - Interpolation mode:
;                  |0 - Default interpolation mode
;                  |1 - Low-quality mode
;                  |2 - High-quality mode
;                  |3 - Bilinear interpolation. No prefiltering is done
;                  |4 - Bicubic interpolation. No prefiltering is done
;                  |5 - Nearest-neighbor interpolation
;                  |6 - High-quality, bilinear interpolation. Prefiltering is performed to ensure high-quality shrinking
;                  |7 - High-quality, bicubic interpolation. Prefiltering is performed to ensure high-quality shrinking
; Return values .: Success      - True
;                  Failure      - False and either:
;                  |@error and @extended are set if DllCall failed
;                  |$GDIP_STATUS contains a non zero value specifying the error code
; Remarks .......: The interpolation mode determines the algorithm that is used when images are scaled or rotated
; Related .......: _GDIPlus_GraphicsGetInterpolationMode
; Link ..........; @@MsdnLink@@ GdipSetInterpolationMode
; Example .......; No
; ===============================================================================================================================
Func _GDIPlus_GraphicsSetInterpolationMode($hGraphics, $iInterpolationMode)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetInterpolationMode", "handle", $hGraphics, "int", $iInterpolationMode)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsSetInterpolationMode

This function is useful when you want to resize an image. This can increase the image quality afterwards.

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