Image rotation using RotateTransform()????
-
Hi, I'm about to rotate an image for display and I used RotateTransform() thanks to Mark Salsbery but now I'm having difficulty using it. When I draw Image without RotateTransform, g.DrawImage(&img,100,100,img.GetWidth(),img.GetHeight()); there's no problem. But when i added RotateTransform. g.RotateTransform(330); // 30o elevation g.DrawImage(&img,100,100,img.GetWidth(),img.GetHeight()); the image is displayed not in point (100,100) as expected rather in (137,37). Is there something wrong in my code? Do I have to add additional code for RotateTransform to work? How will I display a rotated image in a fixed point? Please help me. Thanks.
-
Hi, I'm about to rotate an image for display and I used RotateTransform() thanks to Mark Salsbery but now I'm having difficulty using it. When I draw Image without RotateTransform, g.DrawImage(&img,100,100,img.GetWidth(),img.GetHeight()); there's no problem. But when i added RotateTransform. g.RotateTransform(330); // 30o elevation g.DrawImage(&img,100,100,img.GetWidth(),img.GetHeight()); the image is displayed not in point (100,100) as expected rather in (137,37). Is there something wrong in my code? Do I have to add additional code for RotateTransform to work? How will I display a rotated image in a fixed point? Please help me. Thanks.
You could use Graphics::TranslateTransform() to shift the image vertically and/or horizontally. Note that the order you set transforms with matrixes is important... Here's some reference material: Transformations (GDI+)[^] FWIW, Here's an example (I've posted here before) which rotates an image around its center point, like a propeller...
Gdiplus::Bitmap SrcBitmap(L"C:\\test.tif", FALSE);
Graphics DstGraphics(*this);REAL angle = 0.0f;
for (int i = 0; i < 1440; ++i)
{
DstGraphics.ResetTransform();
DstGraphics.RotateTransform(angle);
DstGraphics.TranslateTransform(SrcBitmap.GetWidth() / 2.0f, SrcBitmap.GetHeight() / 2.0f, MatrixOrderAppend);
DstGraphics.DrawImage(&SrcBitmap, -((INT)SrcBitmap.GetWidth() / 2), -((INT)SrcBitmap.GetHeight() / 2), SrcBitmap.GetWidth(), SrcBitmap.GetHeight());angle += 0.5f;
// Use this to slow it down :)
// ::Sleep(10);
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You could use Graphics::TranslateTransform() to shift the image vertically and/or horizontally. Note that the order you set transforms with matrixes is important... Here's some reference material: Transformations (GDI+)[^] FWIW, Here's an example (I've posted here before) which rotates an image around its center point, like a propeller...
Gdiplus::Bitmap SrcBitmap(L"C:\\test.tif", FALSE);
Graphics DstGraphics(*this);REAL angle = 0.0f;
for (int i = 0; i < 1440; ++i)
{
DstGraphics.ResetTransform();
DstGraphics.RotateTransform(angle);
DstGraphics.TranslateTransform(SrcBitmap.GetWidth() / 2.0f, SrcBitmap.GetHeight() / 2.0f, MatrixOrderAppend);
DstGraphics.DrawImage(&SrcBitmap, -((INT)SrcBitmap.GetWidth() / 2), -((INT)SrcBitmap.GetHeight() / 2), SrcBitmap.GetWidth(), SrcBitmap.GetHeight());angle += 0.5f;
// Use this to slow it down :)
// ::Sleep(10);
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks a lot. Now I will try it.
-
You could use Graphics::TranslateTransform() to shift the image vertically and/or horizontally. Note that the order you set transforms with matrixes is important... Here's some reference material: Transformations (GDI+)[^] FWIW, Here's an example (I've posted here before) which rotates an image around its center point, like a propeller...
Gdiplus::Bitmap SrcBitmap(L"C:\\test.tif", FALSE);
Graphics DstGraphics(*this);REAL angle = 0.0f;
for (int i = 0; i < 1440; ++i)
{
DstGraphics.ResetTransform();
DstGraphics.RotateTransform(angle);
DstGraphics.TranslateTransform(SrcBitmap.GetWidth() / 2.0f, SrcBitmap.GetHeight() / 2.0f, MatrixOrderAppend);
DstGraphics.DrawImage(&SrcBitmap, -((INT)SrcBitmap.GetWidth() / 2), -((INT)SrcBitmap.GetHeight() / 2), SrcBitmap.GetWidth(), SrcBitmap.GetHeight());angle += 0.5f;
// Use this to slow it down :)
// ::Sleep(10);
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Is there a way where I can position the image at the upper left corner of the image to any point? Like for example position the image corner at point (100,100). (100,100) ....._____ ..../ / / / .../ / / / ..------
-
Is there a way where I can position the image at the upper left corner of the image to any point? Like for example position the image corner at point (100,100). (100,100) ....._____ ..../ / / / .../ / / / ..------
TooShy2Talk wrote:
Like for example position the image corner at point (100,100).
Matrix matrix;
matrix.Translate(100.0f, 100.0f);
matrix.RotateAt(30.0f /*angle to rotate*/, PointF(100.0f, 100.0f), MatrixOrderAppend);
graphics.SetTransform(&matrix);
graphics.DrawImage(&img, PointF(0, 0));
graphics.ResetTransform(); -
TooShy2Talk wrote:
Like for example position the image corner at point (100,100).
Matrix matrix;
matrix.Translate(100.0f, 100.0f);
matrix.RotateAt(30.0f /*angle to rotate*/, PointF(100.0f, 100.0f), MatrixOrderAppend);
graphics.SetTransform(&matrix);
graphics.DrawImage(&img, PointF(0, 0));
graphics.ResetTransform();Thank you for the reply. I will try this.
-
Thank you for the reply. I will try this.
It's up to you to decide what the upper left corner of a rotated rectangular image is and how you want it drawn. You'll have to do a little math, but you have the functions to rotate and position. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: