What is the proper way to turn a bitmap around a certain point, with a certain degree?

119 views Asked by At

I would like to use a picturebox to put together an analog clock. I have set a backgound image for the picturebox, that is going to be the "clock-face". Upon it, I meant to use bitmap images as my hour,-minute, and second-hands. I had looked up a few forums, and MSDN documentations, and my problem was nearly solved, but I got stuck at a certain point. (I am using Visual Studio 2013 and C# form application). I had turned my bitmap into a Graphics object, and after that I set the rotation point with the TranslateTransform class and then rotated it with the RotateTransform class. The I drew the my original bitmap to the Graphics object, and set my picturebox.Image to this bitmap. An is shows up only a certain point of the hand. As I set another value to the degree, it shows another certain part of it. My code looks like:

 private void rotateImg(Bitmap b , int degree) {
        Bitmap bitmap = new Bitmap(b.Width,b.Height);
        Graphics g = Graphics.FromImage(bitmap);
        g.TranslateTransform(bitmap.Width/2,bitmap.Height/2);
        g.RotateTransform(degree);
        g.DrawImage(b, new Point(0,0));
        g.Dispose();
        pictureBox1.Image=bitmap;

    } 

And my result looks like this:

enter image description here

I am very confused with this now, I am not really familiar with C# form applications, so I would approciate any advice, link or whatsoever to carry on with this tiny project.

1

There are 1 answers

2
Idle_Mind On BEST ANSWER

Leave the clock face as the Background, then use the Paint() event of your PictureBox to put the hand on top. Something like...

    private Bitmap HourHand;
    private int HourDegree =45;

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.TranslateTransform(pictureBox1.Width / 2, pictureBox1.Height / 2);
        g.RotateTransform(HourDegree);
        g.DrawImage(HourHand, new Point(0, 0));
    }