旋转Picturebox图像的更新速度不够快

时间:2015-05-27 13:09:07

标签: c# image-rotation

所以我有方向盘我想在拖动它时立即用鼠标转动。

我有一个鼠标按下事件,确保用户实际上点击了方向盘。

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;
    }

然后我有一个鼠标移动事件,找到最后一个点和新点之间的角度并旋转它。

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;  
        }
    }

现在问题是,在我停止移动鼠标之前,车轮图像似乎没有在运行时旋转。因此,如果我旋转90度,图像将立即旋转90度,而不是我想要的平滑运动。有任何建议吗?这可能吗?

我的旋转功能:

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;
    }

0 个答案:

没有答案