Sign in to follow this  
Followers 0
CyberNeo

Compare Paint's Rotated Image with GDI's rotated image

5 posts in this topic

Hi all, 

I have an image and rotate it with both MS Paint and GDI. I want to show that the rotated image from both methods are the same. 

Here is the code I have to rotate image with GDI

#include <GDIPlus.au3>

_GDIPlus_Startup()

$hImage1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Picture.gif")
$hGraphic1 = _GDIPlus_ImageGetGraphicsContext($hImage1)
$hImage2 = _GDIPlus_BitmapCreateFromGraphics(_GDIPlus_ImageGetWidth($hImage1), _GDIPlus_ImageGetHeight($hImage1), $hGraphic1)
$hGraphic2 = _GDIPlus_ImageGetGraphicsContext($hImage2)

$matrix = _GDIPlus_MatrixCreate()
_GDIPlus_MatrixRotate($matrix,90)
_GDIPlus_GraphicsSetTransform($hGraphic2, $matrix)
_GDIPlus_GraphicsDrawImage($hGraphic2, $hImage1, 0, -590)

_GDIPlus_ImageSaveToFile($hImage2, @ScriptDir & "\out.gif")

_GDIPlus_MatrixDispose($matrix)
_GDIPlus_GraphicsDispose($hGraphic1)
_GDIPlus_GraphicsDispose($hGraphic2)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

 _GDIPlus_ShutDown ()

Then I used this code to compare 2 images:

#include <GDIPlus.au3>



_GDIPlus_Startup()

$bm1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Picture1.gif")
$bm2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\out.gif")

if ComparePicture($bm1, $bm2) == True Then
   MsgBox(0, "Test result", "Same image!")
Else
   MsgBox(0, "Test result", "Different image!")
EndIf

_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()

Func ComparePicture($bm1, $bm2)

    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF08INDEXED )
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4


    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF08INDEXED)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)



    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    Return ($call[0]=0)


EndFunc

I tried changing the file type, color depth, etc. but I could not get the code to show that they are the same. When I do not rotate the picture i.e 

_GDIPlus_MatrixRotate($matrix,0)

then it recognize the same image. When I rotate right 90, it doesn't. Does anyone knows what might be going?

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think $GDIP_PXF08INDEXED is modifying the images differently. Try it without setting it and it should work.

Futher you can use this code to flip the image:

$hImage1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Picture1.gif")
_GDIPlus_ImageRotateFlip($hImage1, 1) ;90°

_GDIPlus_ImageSaveToFile($hImage1, @ScriptDir & "\out.gif")

_GDIPlus_ImageDispose($hImage1)

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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

It's always polite to post links to where you've asked the question before. There have been many cases in the past where people have spent time on an answer, only to find it's already been answered elsewhere. It's no issue at all reposting on the forum if you don't get help elsewhere, just please link to it :)

Question on stack overflow: link.

Share this post


Link to post
Share on other sites

@UEZ: Wow, thank you so so much. It works!

@Mat: I'm sorry. I'm new here so I just post both on here and stackoverflow. 

Thank you!

Share this post


Link to post
Share on other sites

No problem at all, just in future please link between the two. 

Now the stackoverflow question can be closed with UEZ's answer, and everyone is happy :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0