Jump to content

Auto Rotate .TIF images


Recommended Posts

Does anyone know of a function that would rotate an image from portrait to landscape? Specifically images within a .TIF file. Thanks in advance.

And another way.

Note:- Read tool tip.

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

Opt('MustDeclareVars', 1)

Local $hGUI1, $hGUI2, $hImage, $hGraphic1, $hGraphicGUI
Local $w, $h, $hBMPBuff, $hGraphic, $tMP, $Flg = 0

Local $Path = FileOpenDialog("Choose Image File", @DesktopDir & "", _
        "Images (*.tif;*.gif;*.jpg;*.png;*.bmp)| All (*.*)", 1, "Test.tif")
If Not @error Then
    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile($Path)
Else
    Exit
EndIf
;$hImage = _GDIPlus_ImageLoadFromFile("Fullpath &filename.ext")
$w = _GDIPlus_ImageGetWidth($hImage)
$h = _GDIPlus_ImageGetHeight($hImage)
;ConsoleWrite("$wx$h = " & $w & "x" & $h & "  $hImage = " & $hImage & @CRLF)
; Create a GUI for the original image
$hGUI1 = GUICreate("Original", $w, $h, 0, 0)
GUISetState()
; Draw original image
$hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI1)
_GDIPlus_GraphicsDrawImage($hGraphic1, $hImage, 0, 0)

; Create a GUI for the zoomed image
$hGUI2 = GUICreate("Rotated", $h, $w, 0, 400)
GUISetState()
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGUI2)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($h, $w, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)

;_GDIPlus_DrawImagePoints($hGraphic, $hImage, 0, 0, 0, $w, $h, 0); Back2front
;_GDIPlus_DrawImagePoints($hGraphic, $hImage, 0, $w, 0, 0, $h, $w); Upside down
_GDIPlus_DrawImagePoints($hGraphic, $hImage, $h, 0, $h, $w, 0, 0); Lanscape
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, ($w - $h * $h / $w) / 2, 0, $h, $w, 0, 0, $h * $w / $h, $w * $w / $h);Trim

GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
; Loop until user exits
Do
    $tMP = _WinAPI_GetMousePos()
    If _WinAPI_WindowFromPoint($tMP) = WinGetHandle($hGUI2) Then
        ToolTip("Ctrl + Left mouse click to save image" & @CRLF & _
                "Ctrl + Alt to rotate image", DllStructGetData($tMP, "X") + 5, DllStructGetData($tMP, "Y") - 20)
    Else
        ToolTip("")
    EndIf
    If _IsPressed("01") And _IsPressed("11") Then;Left click + Ctrl key to save image
        Do
        Until Not _IsPressed("01")
        Local $PathFile = FileSaveDialog("Choose Image File Name", @DesktopDir & "", _
                "Images (*.tif;*.png;*.bmp)| All (*.*)", 1, "Porttif.tif")
        If Not @error Then
            _GDIPlus_ImageSaveToFile($hBMPBuff, $PathFile)
            ShellExecute($PathFile)
        EndIf
    EndIf
    If _IsPressed("11") And _IsPressed("12") Then;Ctrl +Alt keys to rotate image
        Do
        Until Not (_IsPressed("11") And _IsPressed("12"))
        $Flg = Mod($Flg + 1, 4)
        Switch $Flg
            Case 0
                _GDIPlus_DrawImagePoints($hGraphic, $hImage, $h, 0, $h, $w, 0, 0); Lanscape
                _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
            Case 1
                _GDIPlus_DrawImagePoints($hGraphic, $hImage, 0, $w, 0, 0, $h, $w); Upside down
                _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
            Case 2
                _GDIPlus_DrawImagePoints($hGraphic, $hImage, 0, 0, 0, $w, $h, 0); Back2front
                _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
            Case 3
                _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage, ($w - $h * $h / $w) / 2, 0, $h, $w, 0, 0, $h * $w / $h, $w * $w / $h);Trim
                _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
        EndSwitch
    EndIf
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

; Release resources
_GDIPlus_GraphicsDispose($hGraphic1)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_GraphicsDispose($hGraphicGUI)
_WinAPI_DeleteObject($hBMPBuff)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
; Check, if the GUI with the Graphic should be repainted
    If $hWnd = $hGUI2 Then _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    If $hWnd = $hGUI1 Then _GDIPlus_GraphicsDrawImage($hGraphic1, $hImage, 0, 0)
    Return $GUI_RUNDEFMSG
EndFunc  ;==>MY_PAINT
;
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...