save rotated image with GDIP Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

save rotated image with GDIP

Post by Sam_ » 30 May 2018, 14:15

Code: Select all

#Include, lib\Gdip_All.ahk

Gui, ChildOrigin: -Caption +E0x80000 +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, ChildOrigin: +HwndChildhwnd
Gui, ChildOrigin: Show, NA


pToken:=Gdip_Startup()
pBitmap:=Gdip_CreateBitmapFromFile(A_ScriptDir "\SomeFile.png")

w:=h:=0, OriginRud:=45
Gdip_GetImageDimensions(pBitmap,w,h)
Gdip_GetRotatedDimensions(w,h,OriginRud,rw,rh)
rw:=(rw>w?rw:w)
rh:=(rh>h?rh:h)

hbm:=CreateDIBSection(rw,rh), hdc:=CreateCompatibleDC(), obm:=SelectObject(hdc,hbm), G:=Gdip_GraphicsFromHDC(hdc)

Gdip_GraphicsClear(G)
Gdip_ResetWorldTransform(G)
Gdip_TranslateWorldTransform(G, rw//2, rh//2)
Gdip_RotateWorldTransform(G,OriginRud)
Gdip_TranslateWorldTransform(G, -rw//2, -rh//2)
Gdip_DrawImage(G, pBitmap, (rw-w)//2, (rh-h)//2, w, h)
UpdateLayeredWindow(Childhwnd, hdc, 200-rw//2, 200-rh//2, rw, rh)

Gdip_SaveBitmapToFile(pBitmap,A_ScriptDir "\Rotated.png")

SelectObject(hdc,obm), DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

Return
This code loads an image from disk, rotates it 45*, displays it on screen to show that the rotation worked properly, and then saves the bitmap to file. The problem is the saved image is not rotated. How would I go about saving the rotated image to disk? I would really prefer not to have to render the image on screen in order to capture the rotation.

TIA,
Sam.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: save rotated image with GDIP  Topic is solved

Post by swagfag » 30 May 2018, 16:28

Code: Select all

#Include Gdip_All.ahk

Gui, ChildOrigin: -Caption +E0x80000 +AlwaysOnTop +ToolWindow +OwnDialogs
Gui, ChildOrigin: +HwndChildhwnd
Gui, ChildOrigin: Show, NA


pToken:=Gdip_Startup()
pBitmap:=Gdip_CreateBitmapFromFile("D:\test.png")

w:=h:=0, OriginRud:=45
Gdip_GetImageDimensions(pBitmap,w,h)
Gdip_GetRotatedDimensions(w,h,OriginRud,rw,rh)
rw:=(rw>w?rw:w)
rh:=(rh>h?rh:h)

hbm:=CreateDIBSection(rw,rh), hdc:=CreateCompatibleDC(), obm:=SelectObject(hdc,hbm), G:=Gdip_GraphicsFromHDC(hdc) 

; Gdip_GraphicsClear(G)
; Gdip_ResetWorldTransform(G)
Gdip_TranslateWorldTransform(G, rw//2, rh//2)
Gdip_RotateWorldTransform(G,OriginRud)
Gdip_TranslateWorldTransform(G, -rw//2, -rh//2)
Gdip_DrawImage(G, pBitmap, (rw-w)//2, (rh-h)//2, w, h)
UpdateLayeredWindow(Childhwnd, hdc, 200-rw//2, 200-rh//2, rw, rh)

; new 'canvas' 
pBitmapRotated := Gdip_CreateBitmap(rw, rh)
pGraphicsRotated := Gdip_GraphicsFromImage(pBitmapRotated)

; reapply tranformations and save it
Gdip_TranslateWorldTransform(pGraphicsRotated, rw//2, rh//2)
Gdip_RotateWorldTransform(pGraphicsRotated,OriginRud)
Gdip_TranslateWorldTransform(pGraphicsRotated, -rw//2, -rh//2)
Gdip_DrawImage(pGraphicsRotated, pBitmap, (rw-w)//2, (rh-h)//2, w, h)
Gdip_SaveBitmapToFile(pBitmapRotated,"D:\test_rotated.png")

SelectObject(hdc,obm)
DeleteObject(hbm)
DeleteDC(hdc)

Gdip_DeleteGraphics(G)
Gdip_DeleteGraphics(pGraphicsRotated)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

Return

z::ExitApp

Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: save rotated image with GDIP

Post by Sam_ » 30 May 2018, 17:07

@swagfag
Brilliant, thank you! Do you happen to know the best way to prevent anti-aliasing? What I've found so far is

Code: Select all

Gdip_SetInterpolationMode(pGraphicsRotated,5) ; NearestNeighbor = 5
but results aren't great on non-90* rotations...

Post Reply

Return to “Ask for Help (v1)”