Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Image rotation using RotateTransform()????

Image rotation using RotateTransform()????

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TooShy2Talk
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • T TooShy2Talk

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      T 2 Replies Last reply
      0
      • M Mark Salsbery

        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:

        T Offline
        T Offline
        TooShy2Talk
        wrote on last edited by
        #3

        Thanks a lot. Now I will try it.

        1 Reply Last reply
        0
        • M Mark Salsbery

          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:

          T Offline
          T Offline
          TooShy2Talk
          wrote on last edited by
          #4

          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) ....._____ ..../ / / / .../ / / / ..------

          R 1 Reply Last reply
          0
          • T TooShy2Talk

            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) ....._____ ..../ / / / .../ / / / ..------

            R Offline
            R Offline
            Rajkumar R
            wrote on last edited by
            #5

            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();

            T 1 Reply Last reply
            0
            • R Rajkumar R

              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();

              T Offline
              T Offline
              TooShy2Talk
              wrote on last edited by
              #6

              Thank you for the reply. I will try this.

              M 1 Reply Last reply
              0
              • T TooShy2Talk

                Thank you for the reply. I will try this.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups