CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Question How to rotate a bitmap image cleanly in GDI+ without a string of images appearing?

    I have drawn a bitmap image using GDI+ in C# on a windows form and would like to rotate the image while the up arrow key is pressed.

    The effect I would like to get is to see the image move in a circular motion while it rotates. The rotation angle is incremented by 5 degrees each time the key pressed event handler executes.

    The problem is that while the image does move in a circular path, a string of previously rotated images appear along the rotation path instead of just the latest rotated image.

    i.e. If the maximum angle is set to 20 degrees, 4 images will be on the screen with the first rotated at 5 degrees, the next at 10 degrees and so on.

    How do I just show a single bitmap image rotating instead of a string of previous images showing up along the rotation path?

    The code is shown below:

    Code:
    (in the form1 load event)
    Graphics g = CreateGraphics();            
    granade = new Bitmap("granade.jpg");
    
    (in the form1 paint event) 
    g.DrawImage(granade,0,0,20,20);
    
    (in the key down event)
    Matrix rotMatrix = new Matrix();
    rotateAngle -= 5;
    
    rotMatrix.RotateAt(rotateAngle);
    g.Transform = rotMatrix;

  2. #2
    Join Date
    Sep 2008
    Posts
    7

    Re: How to rotate a bitmap image cleanly in GDI+ without a string of images appearing?

    You need to draw into a window sized bit map for each frame (double buffering). For each frame, clear the offscreen bitmap, draw the rotated image into the bitmap and then copy it to the window.

  3. #3
    Join Date
    Apr 2005
    Posts
    200

    Re: How to rotate a bitmap image cleanly in GDI+ without a string of images appearing?

    Quote Originally Posted by maspeir
    You need to draw into a window sized bit map for each frame (double buffering). For each frame, clear the offscreen bitmap, draw the rotated image into the bitmap and then copy it to the window.
    Is it to create a new window sized bitmap for copying and clearing for each frame?

    Is there an alternative method to this as the window already has 2 bitmaps, one of which is a 2D terrain which is generated by an algorithm. Having to regenerate the terrain for each frame is too slow.

    I noticed that there is something like a "copy from background" method where pixels from the background are copied onto the bitmap. Could this method work where for each frame, the background overwrites the previous bitmap?

  4. #4
    Join Date
    Sep 2008
    Posts
    7

    Re: How to rotate a bitmap image cleanly in GDI+ without a string of images appearing

    The offscreen bitmap only needs to be big enough to encompass the animation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured