Sign in to follow this  

VB6 - GDIPLUS: how rotate an image on center?

Recommended Posts

how can i rotate an image on it's center?

Dim i As Integer
        For i = 0 To 360
            GdipTranslateWorldTransform hGraphics, 100, 100, MatrixOrderAppend
            GdipRotateWorldTransform hGraphics, 45!, MatrixOrderPrepend
            GdipTranslateWorldTransform hGraphics, -100, -100, MatrixOrderAppend
            GdipDrawImageRectRect hGraphics, HBITMAP, 100, 100, lngwidth, lngheight, 0, 0, lngwidth, lngheight, UnitPixel, ImageAttributes
            Sleep 500
        Next

that code can draw the image rotated. but it's drawed on several positions instead on it's center :(

can anyone advice me?

Share this post


Link to post
Share on other sites

Never done VB6 or GDIPLUS, but the normal trick is to first translate the center of the image to the origin, then rotate, then translate back to were it was.

(Rotation rotates around origin, so if your image is not there, it moves.)

Share this post


Link to post
Share on other sites
Is this happening because it's not erasing the old image?

Because it's not erasing, you get several of the image rotated around a pivot point.

On an important side note is there a legitimate reason you are using vb6, which hasnt been supported by Microsoft for decades, when there are better free options like visual studio community 2015?

Share this post


Link to post
Share on other sites

Why are you translating to (100,100)? You first need to translate to the origin (0,0), rotate and then translate back to the original position. Rotation is based around (0,0) unless the math library does some really weird and unexpected work in the background.

 

[edit] Is the initial position at (-100,-100)? That's the only way I can make sense of this.

Edited by MarkS

Share this post


Link to post
Share on other sites

i'm sorry... the 100X100 is the position where i must draw the image. so the 1st image is drawed on correct position. the next image isn't.

i'm trying learning, from several places, how use the rotate. but seems so complicate without understand it :(

Share this post


Link to post
Share on other sites

Ok, let's make it really simple. In 2D, I have a point at (100, 0), and I want to rotate it 90 degrees around the origin. Where does it end up?

 

I am guessing you say (0, 100).  [The other option is that you say (0, -100), and that's fine too, just invert all my y coordinates then.]

 

 

Now I have a second point (120, 0). No surprise I hope that it ends up at (0, 120).

Now do the same for point (100, 10). The answer is (-10, 100).

Now do the same for point (120, 10). The answer is (-10, 120).

 

You agree for each of the 4 points that my answer is correct?

 

Now imagine the corners of your image are at (100, 0), (120, 0), (120, 10), (100, 10). Where does the image end up after rotation?

Share this post


Link to post
Share on other sites

thinking on (100,0) :

 

X= 100*cos (90) - 0*sin (90)
X= 100 * 0  - 100 * 0 * 1
X= 0

Y= 0 * cos (90) + 100 sin (90)
Y= 100 * 0 - 100 * 0 * 1
Y= 0

 

x' = x * cos (angle) - y * sin (angle)
y' = y* cos( angle) + x sin (angle)

 

what you think?

(100,0) with 90 degrees, will be (0,0)

Share this post


Link to post
Share on other sites

So all points at the x-axis all end up at (0, 0) ?

Does that make sense to you as a rotation?

 

 

 

Y= 0 * cos (90) + 100 sin (90)

Y= 100 * 0 - 100 * 0 * 1

Y= 0

Where is the "* 0" in "100 * 0 * 1" coming from ?

 

Edit: Also the "+" becomes a "-"  :)

Edited by Alberth

Share this post


Link to post
Share on other sites

No problem :)

While math should generally be taken as you do, just run the computations. 2D math is generally an exception, it is often very useful to draw these things on paper, so you can really see how rotate, scale, or translate works.

 

Eg a rotation picture from wikipedia:

https://en.wikipedia.org/wiki/File:Rotation_illustration2.svg

Share this post


Link to post
Share on other sites

Rotation is always around the origin.

What you have to do, is

- move (ie translate) the point that should not move (ie the center of your image) to (0, 0). That means the corners of your image become symmetrical aorund the origin.

- rotate (around (0, 0)). The corners of the image move, the center point does not.

- move back, ie do the opposite translation from the one above.

 

Edit:

If I wanted to rotate my rectangle around the center (110, 5), I'd have to do

- translate the rectangle by (-110, -5)

- rotate 90 degrees

- translate the rectangle by (110, 5)

Edited by Alberth

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