-
Recently Browsing 0 members
No registered users viewing this page.
-
Similar Content
-
By c.haslam
UEZ has kindly provided me with code that rotates an image per a line: see here
Because I want to trim a photograph after rotation, I need to crop the photo. I wrote code which calls _GDIPlus_GraphicsSetClipRect() to do this, but it places spurious stuff in the corners: see 2.jpg attached.
I want only the trimmed photo to show.
The original jpeg is also attached.
The arguments to _GDIPlus_GraphicsSetClipRect() come from solving 2 simultaneous equations: if a and b are the width and height of the original photo, find x and y, the width and height of the rotated and cropped rectangle;
a = x cos(ang) + y sin(ang)
b = x sin(ang) + y cos(ang)
My solution is:
x = (b * sin(ang) - a*cos(ang)) / (sin(ang)^2 - (cos(ang)^2)
y = (b * cos(ang) - a * sin(ang)) / (cos(ang)^2 - sin(ang)^2)
To try to get a handle on the problem, I have temporarily added in several lines in Case $btn. In 2.jpg, you can see that the rectangle bounding the area to be retained is in the wrong place.
The code is:
#include <ButtonConstants.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Opt('MustDeclareVars',1) Global Const $MK_SHIFT = 0x4 Global Const $MK_CONTROL = 0x8 Global Const $fPi = ACos(-1), $fPi2 = $fPi / 2, $fRad = 180 / $fPi GUIRegisterMsg($WM_MOUSEWHEEL, "WM_MOUSEWHEEL") GUIRegisterMsg($WM_PAINT,'WM_PAINT') Global $gX0=10,$gX1=580,$gY0,$gY1,$gForm1,$glblPic,$iLW,$iLH,$iW,$iH Global $ghCanvas,$ghImage,$ghPen,$gGraphicPic,$ghBitmap,$ghMatrix,$ghImageClone,$ghGfxClone Global Const $kDegToRads = 3.14159/180 main() Func main() $gForm1 = GUICreate("Form1", 623, 601, 192, 114) $iLW = 589 $iLH = 500 $glblPic = GUICtrlCreateLabel("", 8, 8, $iLW, $iLH) Local $btn = GUICtrlCreateButton("Rotate", 472, 560, 65, 25) GUISetState(@SW_SHOW) Local $oldY0,$oldY1,$fAngle _GDIPlus_Startup() $ghPen = _GDIPlus_PenCreate(0xFF999999,2) $ghImage = _GDIPlus_ImageLoadFromFile('H:\b\pergola.jpg') Local $h = GUICtrlGetHandle($glblPic) $gGraphicPic = _GDIPlus_GraphicsCreateFromHWND($h) $gY0 = 400 $gY1 = 400 $iW = _GDIPlus_ImageGetWidth($ghImage) $iH = _GDIPlus_ImageGetHeight($ghImage) $ghBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH) $ghCanvas = _GDIPlus_ImageGetGraphicsContext($ghBitmap) $ghMatrix = _GDIPlus_MatrixCreate() ; Loop until the user exits. While True If $gY0<>$oldY0 Or $gY1<>$oldY1 Then Paint() $oldY0 = $gY0 $oldY1 = $gY1 Else Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $btn $ghImageClone=_GDIPlus_BitmapCreateFromScan0($iW, $iH) $ghGfxClone=_GDIPlus_ImageGetGraphicsContext($ghImageClone) _GDIPlus_MatrixTranslate($ghMatrix, $iW/2, $iH/2) $gY1 = 300 ; temp Local $ang = -Angle($gY1-$gY0, $gX1-$gX0) _GDIPlus_MatrixRotate($ghMatrix, $ang) ;~ _GDIPlus_MatrixRotate($ghMatrix, -Angle($gY1-$gY0, $gX1-$gX0)) _GDIPlus_MatrixTranslate($ghMatrix, -$iW/2, -$iH/2) _GDIPlus_GraphicsSetTransform($ghGfxClone, $ghMatrix) _GDIPlus_GraphicsDrawImageRect($ghGfxClone,$ghImage,0,0,$iW,$iH) Local $angRads = $ang*$kDegToRads Local $sinAng = Sin($angRads) Local $cosAng = Cos($angRads) Local $wid = ($iLH*$sinAng-$iLW*$cosAng)/($sinAng^2-$cosAng^2) Local $left = ($iLW-$wid)/2 Local $ht = ($iLH*$cosAng-$iLW*$sinAng)/($cosAng^2-$sinAng^2) Local $top = ($iLH-$ht)/2 _GDIPlus_GraphicsDrawRect($ghGfxClone,$left,$top,$wid,$ht,$ghPen) ; temp _GDIPlus_GraphicsSetClipRect($ghGfxClone,$left,$top,$wid,$ht,0) _GDIPlus_GraphicsDispose($ghGfxClone) $ghGfxClone=_GDIPlus_ImageGetGraphicsContext($ghImage) _GDIPlus_GraphicsClear($ghImage) _GDIPlus_GraphicsDrawImageRect($ghGfxClone,$ghImageClone,0,0,$iW,$iH) _GDIPlus_GraphicsDispose($ghGfxClone) _GDIPlus_ImageDispose($ghImageClone) $gy0 = 400 $gy1 = 400 Paint() _GDIPlus_ImageSaveToFile($ghImage,'H:\b\2.jpg') ; added - is rotated, with same size as original file EndSwitch EndIf WEnd ; Clean up resources _GDIPlus_MatrixDispose($ghMatrix) _GDIPlus_ImageDispose($ghCanvas) _GDIPlus_ImageDispose($ghImage) _GDIPlus_ImageDispose($ghBitmap) _GDIPlus_PenDispose($ghPen) _GDIPlus_GraphicsDispose($gGraphicPic) _GDIPlus_Shutdown() EndFunc Func WM_MOUSEWHEEL($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam, $lParam Local Const $kYmax=$iLH-1,$kDelta=2 Local $vec = GUIGetCursorInfo($gForm1) If $vec[4] = $vec[4]=$glblPic Then Local $iDelta = BitShift($wParam, 16) ; positive = up Local $iKeys = _WinAPI_LoWord($wParam) If BitAND($iKeys,$MK_CONTROL)=$MK_CONTROL Then If BitAND($iKeys,$MK_SHIFT)=$MK_SHIFT Then ; do nothing Else If $iDelta > 0 And $gY0>3 Then $gY0 -= $kDelta If $iDelta < 0 And $gY0<$kYmax Then $gY0 += $kDelta EndIf Else If BitAND($iKeys,$MK_SHIFT)=$MK_SHIFT Then If $iDelta > 0 And $gY1>3 Then $gY1 -= $kDelta If $iDelta < 0 And $gY1<$kYmax Then $gY1 += $kDelta Else If $iDelta > 0 And $gY0>3 Then $gY0 -= $kDelta If $iDelta < 0 And $gY0<$kYmax Then $gY0 += $kDelta If $iDelta > 0 And $gY1>3 Then $gY1 -= $kDelta If $iDelta < 0 And $gY1<$kYmax Then $gY1 += $kDelta EndIf EndIf EndIf Return $GUI_RUNDEFMSG EndFunc ;==>WM_MOUSEWHEEL Func Paint() _GDIPlus_GraphicsClear($ghCanvas,0) _GDIPlus_GraphicsDrawImageRect($ghCanvas, $ghImage, 0,0, $iLW, $iLH) _GDIPlus_GraphicsDrawLine($ghCanvas, $gX0, $gY0, $gX1, $gY1, $ghPen) _GDIPlus_GraphicsDrawImageRect($gGraphicPic, $ghBitmap, 0, 0, $iW, $iH) EndFunc Func WM_PAINT() _WinAPI_RedrawWindow($gForm1, 0, 0, $RDW_UPDATENOW) Paint() _WinAPI_RedrawWindow($gForm1, 0, 0, $RDW_VALIDATE) EndFunc Func Angle($y, $x) ;return value is in degree Local Const $fPi = ACos(-1), $fPi2 = ACos(-1) / 2, $fRad = 180 / $fPi Switch True Case ($x > 0) Return ATan($y / $x) * $fRad Case ($x < 0 And $y >= 0) Return ATan($y / $x + $fPi) * $fRad Case ($x < 0 And $y < 0) Return ATan($y / $x - $fPi) * $fRad Case ($x = 0 And $y > 0) Return $fPi2 * $fRad Case ($x = 0 And $y < 0) Return -$fPi2 * $fRad Case ($x = 0 And $y = 0) Return 0 EndSwitch EndFunc I also don't understand how the rotated picture gets displayed. There are graphics objects, graphics contexts and PDI+ bitmaps. How are they related?
Help would be much appreciated.
-
By Alexxander
I have lot's of images like this
http://postimg.org/image/uz24d8z27/
http://postimg.org/image/5isx6mc61/
i would like to crop the page number (the arabic number on the top of the page)
so it would be something like this ...
any ideas ?
-
By GreenCan
This is a visual Crop Tool,
Version 1.0.0.5
Load the image in the Crop GUI Move the cross-hair to the upper left corner where to start cropping Mark the rectangle with left mouse button, release Adjust the position with left mouse, release (right mouse = start over) Press enter Image is saved in same folder, same format, with '_cr' added to file name As simple as that.
Updated script,
Visual Crop UDF 1.0.0.4 examples.au3
Visual Crop UDF 1.0.0.4.au3 (No obscuration of non-selected area)
Visual Crop UDF 1.0.0.5.au3 (with obscuration)
Ver 1.0.0.1
Avoid trespassing the edge of the GUI while selecting the crop area. Ver 1.0.0.4
Mark crop area in any direction Resizing Corner grabbers (use with Ctrl Left Mouse button) Mouse cursor changes over move and resizing corner grabbers Redraw when left mouse clicking outside of crop area (restart crop), right Mouse click, same result. Magnifier de-/selectable with Ctrl-M or F2. (original function by Melba23) Note:
without image ratio: Magnifier remains visible till the crop area has been marked
with image ratio: Magnifier is only visible until the rectangle appears, first corner has been marked.
Ver 1.0.0.5
Obscure non crop area, did some hard thinking and found my way...
Missing still:
? Enjoy
GreenCan
Related:
-
By wakillon
Easily Crop a picture to a wanted dimension
and set your Windows desktop background wallpaper without stretching or distorting it.
Drag'n drop a Picture for load it.
Drag it for position it and use mouse wheel for zoom - unzoom it.(TouchPad users need to plug a Mouse )
Select dimensions and format you want for save your wallpaper.
Pictures with transparency are supported.
By default Pictures are saved on your desktop.
Tips :
Hold Left Ctrl key for move the photo more slowly.
Hold Left Shift key for move the photo more quickly.
Hold Left Shift key for Zoom/UnZoom more quickly.
Hold Left Shift key when drag'n drop photo for work with a best quality. (Moves and Zoom are more slow)
Executable : WallpaperCropper.exe
-
By wakillon
WallpaperCropper
Easily Crop a picture to a wanted Screen dimension
and set your Windows desktop background wallpaper without stretching or distorting it.
Does your desktop background picture look stretched ?
Do you find it time-consuming to crop pictures to the right proportions for your desktop ?
WallpaperCropper is the solution.
Crop and set any picture easily to your desktop dimensions in some few clicks.
Don't waste time loading big, slow photo-editing software to manually crop it or resize it.
Don't stay stuck with the choices Windows gives you !
Drag'n drop a Picture for load it.
Drag it for position it and use mouse wheel for zoom - unzoom it.(TouchPad users need to plug a Mouse )
Select dimensions and format you want for save your wallpaper.
Pictures with transparency are supported.
By default Pictures are saved on your desktop.
Tips :
Hold Left Ctrl key for move the photo more slowly.
Hold Left Shift key for move the photo more quickly.
Hold Left Shift key for Zoom/UnZoom more quickly.
Hold Left Shift key when drag'n drop photo for work with a best quality. (Moves and Zoom are more slow)
All externals files are included in script, and all Buttons were made online with chimply.com the easy and free buttons Generator !
Previous downloads : 70
source : WallpaperCropper 1.0.1.5.au3
executable : WallpaperCropper.exe.html
(Once this html file downloaded, double click on it for start the download)
Hope you will like it !
-