Jump to content
Sign in to follow this  
ChrisL

GDIPlus Skewing an Image

Recommended Posts

ChrisL

While this works I'm not convinced its the best way to do it.

It opens an image file.

Draws (squashes)the image to a 200x200 square in memory

Then draws a skewed image for left and right.

Eventually this is for a Photo viewer and the skewed left and right image will be for previous and next image, so I will have to have it as a function that returns the individual left or right image as a bitmap in memory.

The other thing is I need to draw a box/border around the skewed image and I'm not sure the best way to go about that either.

Any help and advise please.

Thanks

Chris

#include <GDIPlus.au3>


Global $width=500
Global $height=300
$hwnd=GUICreate("Skew",$width,$height)
GUISetState()


_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
_GDIPlus_GraphicsClear($graphics,0xFF008800)
$image=_GDIPlus_ImageLoadFromFile("D:\My Documents\My Pictures\DSCF0239.JPG")
$iwidth=_GDIPlus_ImageGetWidth($image)
$iheight=_GDIPlus_ImageGetHeight($image)


$h200BMP = _GDIPlus_BitmapCreateFromGraphics(200,200, $graphics)
ConsoleWrite("200x200 BMP Handle = " & $h200BMP & @crlf)
$hGraphicsContext = _GDIPlus_ImageGetGraphicsContext ($h200BMP)
ConsoleWrite("hraphicsContext = " & $hGraphicsContext & @crlf)
$Ret = _GDIPlus_GraphicsDrawImageRectRect($hGraphicsContext,$image,20,20,$iwidth,$iheight,0,0,200,200)
ConsoleWrite("GraphicsDrawImageRectRect = " & $Ret & @crlf)
$hClone = _GDIPlus_BitmapCloneArea($h200BMP,0,0,200,200)
ConsoleWrite("BitmApCloneArea = " & $hClone & @crlf)


$topless = 40;say top is 40 shorter than bottom
$angle = 3

;Right hand Skew
for $iSrcX = 1 to 200 step $angle

    $indent = $topless*(200 - $iSrcX)/200
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX,           $iDstY,    $iDstWidth,   $iDstHeight
    _GDIPlus_GraphicsDrawImageRectRect($graphics,  $hClone,  $iSrcX ,    0 , $iSrcX + 1,    200 ,   320 +$iSrcX/$angle,  50+$indent ,   1 ,        200 - (2*$indent) )

Next


;Left hand Skew
For $iSrcX = 200 to 1 step -$angle

      $indent = $topless*(200 - $iSrcX)/200
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX,        $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX,           $iDstY,    $iDstWidth,   $iDstHeight
    _GDIPlus_GraphicsDrawImageRectRect($graphics,  $hClone,  200 - $iSrcX ,    0 ,  $iSrcX - 1,    200 ,   160-$iSrcX/$angle,  50+$indent ,   1 ,        200 - (2*$indent) )

Next


Do
Until GUIGetMsg()=-3


_GDIPlus_ImageDispose ($hClone)
_GDIPlus_BitmapDispose($h200BMP)
_GDIPlus_ImageDispose($image)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_GraphicsDispose($hGraphicsContext)
_GDIPlus_Shutdown()
Edited by ChrisL

Share this post


Link to post
Share on other sites
UEZ

I do not know any function in GDI+ lib. that can skew an image that way - only _GDIPlus_DrawImagePoints() but this function creates only parallelograms.

For an example of _GDIPlus_DrawImagePoints() have a look e.g. to Rotating Cube 2 + Textures where unfortunately the perspective is not properly because of parallelograms .;)

To draw a box I used this method:

#include <GDIPlus.au3>


Global $width=500
Global $height=300
$hwnd=GUICreate("Skew",$width,$height)
GUISetState()


_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
_GDIPlus_GraphicsClear($graphics,0xFF008800)
$image=_GDIPlus_ImageLoadFromFile("D:\My Documents\My Pictures\DSCF0239.JPG")
$iwidth=_GDIPlus_ImageGetWidth($image)
$iheight=_GDIPlus_ImageGetHeight($image)
$hPen=_GDIPlus_PenCreate(0xF0202060,16) ;create a pen

$h200BMP = _GDIPlus_BitmapCreateFromGraphics(200,200, $graphics)
ConsoleWrite("200x200 BMP Handle = " & $h200BMP & @crlf)
$hGraphicsContext = _GDIPlus_ImageGetGraphicsContext ($h200BMP)
_GDIPlus_GraphicsSetSmoothingMode($hGraphicsContext, 2)

ConsoleWrite("hraphicsContext = " & $hGraphicsContext & @crlf)
$Ret = _GDIPlus_GraphicsDrawImageRectRect($hGraphicsContext,$image,0,0,$iwidth,$iheight,0,0,200,200)
_GDIPlus_GraphicsDrawRect($hGraphicsContext,0,0,200,200,$hPen) ;and draw a rect. over the image


ConsoleWrite("GraphicsDrawImageRectRect = " & $Ret & @crlf)
$hClone = _GDIPlus_BitmapCloneArea($h200BMP,0,0,200,200)
ConsoleWrite("BitmApCloneArea = " & $hClone & @crlf)


$topless = 40;say top is 40 shorter than bottom
$angle = 3

;Right hand Skew
for $iSrcX = 1 to 200 step $angle

    $indent = $topless*(200 - $iSrcX)/200
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX,   $iDstY, $iDstWidth, $iDstHeight
    _GDIPlus_GraphicsDrawImageRectRect($graphics, $hClone, $iSrcX , 0 , $iSrcX + 1, 200 , 320 +$iSrcX/$angle, 50+$indent , 1 ,  200 - (2*$indent) )

Next


;Left hand Skew
For $iSrcX = 200 to 1 step -$angle

    $indent = $topless*(200 - $iSrcX)/200
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX,    $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX,    $iDstY, $iDstWidth, $iDstHeight
    _GDIPlus_GraphicsDrawImageRectRect($graphics, $hClone, 200 - $iSrcX ,   0 , $iSrcX - 1, 200 , 160-$iSrcX/$angle, 50+$indent , 1 ,   200 - (2*$indent) )

Next


Do
Until GUIGetMsg()=-3

_GDIPlus_PenDispose($hPen)
_GDIPlus_ImageDispose ($hClone)
_GDIPlus_BitmapDispose($h200BMP)
_GDIPlus_ImageDispose($image)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_GraphicsDispose($hGraphicsContext)
_GDIPlus_Shutdown()

Maybe some GDI+ cracks may have a fast solution...

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
ChrisL
ChrisL

I have this working quite well now..

#include <GDIPlus.au3>


Global $width=550
Global $height=700

$hwnd=GUICreate("Skew",$width,$height)

GUISetState()


_GDIPlus_Startup()
$hgraphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
_GDIPlus_GraphicsClear($hgraphics)


_SkewImage("D:\My Documents\My Pictures\DSCF0239.JPG",360,50,$hgraphics,"Right",120,600,60)
_SkewImage("D:\My Documents\My Pictures\DSCF0239.JPG",60,50,$hgraphics,"Left",120,600,60)

Do
Until GUIGetMsg()=-3


_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()




Func _SkewImage($sPath,$DrawX, $DrawY, $graphics, $Left_Or_Right, $SkewWidth = 300, $SkewHeight = 300,  $AnglePixels = 40, $BorderWidth = 5 )

    Local $SkewHImage, $SkewImageWidth, $SkewImageHeight, $hSkewPen, $hSkewBMP, $Ret, $hSkewClone, $indent

    $SkewHImage=_GDIPlus_ImageLoadFromFile($sPath)
    $SkewImageWidth=_GDIPlus_ImageGetWidth($SkewHImage)
    $SkewImageHeight=_GDIPlus_ImageGetHeight($SkewHImage)
    $hSkewPen =_GDIPlus_PenCreate(0xFFFFFFFF,$BorderWidth)

    $hSkewBMP = _GDIPlus_BitmapCreateFromGraphics($SkewWidth,$SkewHeight, $graphics)
    ConsoleWrite("200x200 BMP Handle = " & $hSkewBMP & @crlf)
    $hSkewGraphicsContext = _GDIPlus_ImageGetGraphicsContext ($hSkewBMP)
    _GDIPlus_GraphicsSetSmoothingMode($hSkewGraphicsContext, 2)

    ConsoleWrite("hraphicsContext = " & $hSkewGraphicsContext & @crlf)
    $Ret = _GDIPlus_GraphicsDrawImageRectRect($hSkewGraphicsContext,$SkewHImage,0,0,$SkewImageWidth,$SkewImageHeight,0,0,$SkewWidth,$SkewHeight)
    If $BorderWidth > 0 then _GDIPlus_GraphicsDrawRect($hSkewGraphicsContext,0,0,$SkewWidth,$SkewHeight,$hSkewPen) ;and draw a rect. over the image


    ConsoleWrite("GraphicsDrawImageRectRect = " & $Ret & @crlf)
    $hSkewClone = _GDIPlus_BitmapCloneArea($hSkewBMP,0,0,$SkewWidth,$SkewHeight)
    ConsoleWrite("BitmApCloneArea = " & $hSkewClone & @crlf)


    If $Left_Or_Right = "Right" then
        for $iSrcX = 1 to $SkewWidth step 1

            $indent = $AnglePixels*($SkewWidth - $iSrcX)/$SkewWidth

            ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics,$hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight,        $iDstX,             $iDstY,    $iDstWidth, $iDstHeight
            _GDIPlus_GraphicsDrawImageRectRect($graphics, $hSkewClone,  $iSrcX ,  0 ,   $iSrcX + 1, $SkewHeight , $DrawX +$iSrcX, $DrawY+$indent , 1 ,        $SkewHeight - (2*$indent) )
            ;Sleep(1)
        Next
    Else

        For $iSrcX = 1 to $SkewWidth step 1

            $indent = -($AnglePixels*($SkewWidth - $iSrcX)/$SkewWidth - $AnglePixels)
            ConsoleWrite($indent & @crlf)
            ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage,    $iSrcX, $iSrcY, $iSrcWidth,  $iSrcHeight,          $iDstX,                 $iDstY,    $iDstWidth,   $iDstHeight
            _GDIPlus_GraphicsDrawImageRectRect($graphics, $hSkewClone,  $iSrcX ,  0 ,   $iSrcX + 1, $SkewHeight , $DrawX + $iSrcX,  $DrawY + $indent , 1 ,        $SkewHeight - (2*$indent) )
            ;_GDIPlus_GraphicsDrawImageRectRect($graphics, $hSkewClone,  $iSrcX ,   0 ,  $iSrcX + 1,  $SkewHeight ,   $DrawX + ($iSrcX/$Step), ($DrawY*2)-$indent , 1 ,   $SkewHeight - (2*$indent)  )
        ;sleep(10)
        Next

    EndIf

    _GDIPlus_PenDispose($hSkewPen)
    _GDIPlus_ImageDispose ($hSkewClone)
    _GDIPlus_BitmapDispose($hSkewBMP)
    _GDIPlus_ImageDispose($SkewHImage)
    _GDIPlus_GraphicsDispose($hSkewGraphicsContext)

EndFunc

Share this post


Link to post
Share on other sites
ChrisL

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
Sign in to follow this  

×