2

So I have steering wheel I want to turn instantly by mouse when dragging it.

I have a mouse down event that makes sure the user is actually clicking on the steering wheel.

private void HelmPb_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        PictureBox p = (PictureBox) sender;
        Bitmap bmp = (Bitmap) p.Image;
        //GetPixel works on initial image, not stretched one.
        double ratio = (double) p.Image.Width/p.Width;
        //Ignore the non-helm portion of the helm.
        Color trans = bmp.GetPixel(Convert.ToInt32(ratio*e.X), Convert.ToInt32(ratio*e.Y));
        if (trans.A.Equals(0))
        {
            _dontTurn = true;
            return;
        }
        _dontTurn = false;
        _firstHelmPoint = MousePosition;
    }

Then I have a mouse move event that find the angle between the last point and the new point and rotate it.

private void HelmPb_MouseMove(object sender, MouseEventArgs e)
    {
        PictureBox p = (PictureBox)sender;
        bool inside = p.ClientRectangle.Contains(p.PointToClient(MousePosition));
        if ((e.Button != MouseButtons.Left) || _dontTurn || !inside) return;
        Point temp = MousePosition;
        int changeX = temp.X - _firstHelmPoint.X;
        int changeY = temp.Y - _firstHelmPoint.Y;
        double hypotenuse = Math.Sqrt((Math.Pow(changeX, 2)) + Math.Pow(changeY, 2));
        int c = (int) hypotenuse;
        int helmXMid = HelmPb.PointToScreen(Point.Empty).X + (HelmPb.Width/2);
        int helmYMid = HelmPb.PointToScreen(Point.Empty).Y + (HelmPb.Height/2);
        int d = temp.X - helmXMid;
        int a = Math.Abs(helmXMid - _firstHelmPoint.X);
        hypotenuse = Math.Sqrt(Math.Pow(Math.Abs(d), 2) + Math.Pow(changeY, 2));
        int b = Convert.ToInt32(hypotenuse);
        double j = (Math.Pow(a, 2) + (Math.Pow(b, 2)) - Math.Pow(c, 2));
        double angle = Math.Acos(j/(2*a*b));
        using (Bitmap bmp = new Bitmap(Properties.Resources.Helm))
        {
            float degrees = Convert.ToSingle(angle);
            //int cross = (d*changeY - changeY*changeX);
            //int dot = cross*-1;
            //if (dot > 0) _deltaHelmTurn -= degrees;
            _deltaHelmTurn += degrees;
            Bitmap newBmp = RotateImage(bmp, _deltaHelmTurn);
            HelmPb.Image = newBmp;  
        }
    }

Now my problem is, the wheel image doesn't appear to rotate at runtime until I stop moving my mouse. So if I rotate 90 degrees the image will instantly rotate 90 degrees instead of a smooth movement that I want. Any suggestions/is this possible?

My rotate function:

private Bitmap RotateImage(Bitmap b, float angle)
    {
        //Create a new empty bitmap to hold rotated image.
        Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
        //Make a graphics object from the empty bitmap.
        Graphics g = Graphics.FromImage(returnBitmap);
        //move rotation point to center of image.
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.TranslateTransform(b.Width/2F, b.Height/2F);
        //Rotate.
        g.RotateTransform(angle);
        //Move image back.
        g.TranslateTransform(-b.Width/2F, -b.Height/2F);
        //Draw passed in image onto graphics object.
        g.DrawImage(b, new PointF(0.0F, 0.0F));
        return returnBitmap;
    }
Nolemonpledge
  • 139
  • 1
  • 2
  • 11
  • 1
    Not the problem you are having but you are not disposing the old value of `HelmPb.Image` before you assign a new image to it. – Scott Chamberlain May 27 '15 at 13:32
  • Hey Yeah that seems to give me something of what I wanted. Still updating it a little slow but I think I could try and work with this. Thanks man! The refresh!! – Nolemonpledge May 27 '15 at 14:53
  • Hey the link to the post isn't there could use fix it? – Nolemonpledge May 27 '15 at 14:56
  • 1
    Have you tried to insert a HelmPb..Refresh(); after setting the new Image? - And yes, do dispose of the old Bitmaps.. - Also you may want ot have a look at [this post](http://stackoverflow.com/questions/29622850/getting-values-from-mouse-hover-on-a-class-object-c-sharp/29644962?s=1|0.9824#29644962) for a shorter way to get at the angle – BTW, you should prefix the name (or a significant part of the start) of a person you want to talk to with an @ so he get pinged.. – TaW May 27 '15 at 15:16

0 Answers0