Question:
How to rotate a bitmap cleanly in GDI+ without a string of images appearing?
ZhiYi
2008-09-25 08:35:16 UTC
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;

[/code]
Three answers:
Joe R
2008-09-25 09:15:38 UTC
Use Invalidate to clear the drawing surface.

If you only want a portion of the the surface invalidated you can specify the rectangle.

For example, if you are drawing on a form you would call myForm.Invalidate();
spopt36
2008-09-25 08:47:39 UTC
I haven't used GDI in a while, but I think you're missing a call to a function that clears the drawing area before you call Transform(...). I forget what the function is called though. Look through the MSDN

Without calling it, your newly rendered image will just go on top of the previous ones.
?
2017-01-01 23:15:39 UTC
Disgusting vile souls who should be shot (for loss of greater desirable words) in case you do no longer sense like pulling it aside you will desire to get your azz a familiar block of cheese. Idiots! String cheese.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...