GDIPLUS: how can i rotate an image by it's center position?

how can i rotate an image by it's center?
(i have code from another language and i use flat gdiplus functions, but i can translate from C\C++ to another language)
the pseucode:
1 - GdipTranslateWorldTransform(graphics,100 + lngwidth / 2, 100 + lngheight / 2);
2 - GdipRotateWorldTransform(graphics,rotateangle);
3 - GdipTranslateWorldTransform(graphics,-(100 + lngwidth / 2), -(100 + lngheight / 2))(like you see inverted);
4 - GdipDrawImageRectRect(), using the 100,100 position.

by problem is that the imaged is drawed in a circle way, instead on same position.
can anyone advice me?

If I've understood you correctly, you may want to try applying the rotation transformation before the translation transformation.
i'm sorry, but i continue confused. i can't draw it on same position :(
can you show me a C\C++ code for i draw it on same position?
You are doing two things:

1.) You are translating the image with GdipTranslateWorldTransform. This means you are moving the image around.

2.) You are rotating the image with GdipRotateWorldTransform. This means you are rotating the image.


To rotate an image around its center, you need to rotate first, and move it second.
Maybe I'm not understanding you - can you show me a picture of what you want, and a picture of what you're getting?
see the image result:

https://s30.postimg.org/uyaxt1ygx/rotate_problem.jpg

like you see(2 yellow images), they aren't on same position :(
see the code:
1
2
3
4
5
6
7
8
9
Dim i As Single
        For i = 0 To 25
            
            GdipRotateWorldTransform hGraphics, i, MatrixOrderAppend
            GdipTranslateWorldTransform hGraphics, 100, 100, MatrixOrderAppend
            GdipDrawImageRectRect hGraphics, HBITMAP, 100, 100, lngwidth, lngheight, 0, 0, lngwidth, lngheight, UnitPixel, ImageAttributes
            'GdipResetWorldTransform hGraphics
            Sleep 500
        Next 

like you see, i do GdipRotateWorldTransform() before GdipTranslateWorldTransform().
i'm sorry, but or i miss something or something :(
i need that 2 yellow faces on same position
(isn't C\C++ code, but it's easy to understand it)
Last edited on
This should help you, it's for OpenGL, but it should do the trick.
https://learnopengl.com/#!Getting-started/Transformations
@Cambalinho
I think I figured it out. Take a look at what I've written here:

I wrote this in C++, I hope it's useful to you.
I have a function called 'on_paint' where the relevant stuff happens:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void on_paint(HDC hdc) {
	Gdiplus::Graphics graphics(hdc);
	
	Gdiplus::Color blue(255, 0, 0, 255);
	Gdiplus::Pen pen(blue);

	Gdiplus::Image image(L"toast.jpg");

	graphics.DrawImage(&image, 150, 150);
	
	graphics.TranslateTransform(0, 0);
	graphics.DrawLine(&pen, 75, 150, 225, 150);
	graphics.DrawLine(&pen, 150, 75, 150, 225);
	graphics.ResetTransform();

	graphics.TranslateTransform(300, 0);
	graphics.DrawLine(&pen, 75, 150, 225, 150);
	graphics.DrawLine(&pen, 150, 75, 150, 225);
	graphics.ResetTransform();

	graphics.TranslateTransform(300, 300);
	graphics.DrawLine(&pen, 75, 150, 225, 150);
	graphics.DrawLine(&pen, 150, 75, 150, 225);
	graphics.ResetTransform();

	graphics.TranslateTransform(0, 300);
	graphics.DrawLine(&pen, 75, 150, 225, 150);
	graphics.DrawLine(&pen, 150, 75, 150, 225);
	graphics.ResetTransform();

}


Lines 11 - 29 just paint blue crosshairs on our window so we can see how things will move around. Right now all this puts on the screen is this:
https://s23.postimg.org/qx4dd7fez/toast_no_transform.jpg

Now we try to rotate the image by applying the rotation transformation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void on_paint(HDC hdc) {
	Gdiplus::Graphics graphics(hdc);
	
	Gdiplus::Color blue(255, 0, 0, 255);
	Gdiplus::Pen pen(blue);

	Gdiplus::Image image(L"toast.jpg");
	
	graphics.RotateTransform(45.0);

	graphics.DrawImage(&image, 150, 150);
	graphics.ResetTransform();
	
	//... 


I've only added two lines of code. Line 9 is the rotation and line 12 resets the transformation matrix so that the blue crosshairs are unaffected.
However, this doesn't do what we want - this is what we get:
https://s23.postimg.org/6afnrvw0b/toast_bad.jpg


As you can see, the image was rotated by the origin of the window (0, 0) - the top-left corner . We wanted the image to rotate around its own center. This is how we fix it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void on_paint(HDC hdc) {
	Gdiplus::Graphics graphics(hdc);
	
	Gdiplus::Color blue(255, 0, 0, 255);
	Gdiplus::Pen pen(blue);

	Gdiplus::Image image(L"toast.jpg");

	const float angle = 45.0;
	Gdiplus::PointF center(300.0, 300.0);
	Gdiplus::Matrix matrix;
	matrix.RotateAt(angle, center);

	graphics.SetTransform(&matrix);

	graphics.DrawImage(&image, 150, 150);
	graphics.ResetTransform();
	//... 


Which gives us the result we want:
https://s23.postimg.org/f6qfvtmmj/toast_good.jpg

Hope this helped.
Last edited on
it could but the GdipRotateMatrix() Flat function don't accept the 'center':
https://msdn.microsoft.com/en-us/library/windows/desktop/ms534044(v=vs.85).aspx
yah i must use the GDIPLUS Flat API functions
i'm testing more the code. but without sucess :(
i understand that these isn't a VB forum and i'm using the GDIPLUS Flap API functions. but i'm trying :(
Topic archived. No new replies allowed.