在窗体上连续旋转图片

时间:2012-05-03 22:48:20

标签: c# visual-studio-2010 picturebox

我正在尝试使用计时器类和代码项目的组合在图片框控件(Visual Studio 2010 C#)中平滑地旋转图像。我遇到的问题是图片不旋转或我得到线程异常。关于“该物体在其他地方使用”的东西。这是代码的主要部分,我非常感谢您提供的任何帮助。谢谢。

 private void Form1_Load(object sender, EventArgs e)
    {
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Start();
    }

    private void timer_Elapsed(object sender, EventArgs e)
    {
        //Graphics graphic = Graphics.FromImage(pictureBox1.Image); 
        //graphic.RotateTransform(45); 


        this.Invoke(new MethodInvoker(delegate { RotateImage(pictureBox1.Image, 10); }));    

    }



    public static Bitmap RotateImage(Image image, float angle)
    {
        // center of the image
        float rotateAtX = image.Width / 2;
        float rotateAtY = image.Height / 2;
        bool bNoClip = false;
        return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
    }

    public static Bitmap RotateImage(Image image, float angle, bool bNoClip)
    {
        // center of the image
        float rotateAtX = image.Width / 2;
        float rotateAtY = image.Height / 2;
        return RotateImage(image, rotateAtX, rotateAtY, angle, bNoClip);
    }

    public static Bitmap RotateImage(Image image, float rotateAtX, float rotateAtY, float angle, bool bNoClip)
    {
        int W, H, X, Y;
        if (bNoClip)
        {
            double dW = (double)image.Width;
            double dH = (double)image.Height;

            double degrees = Math.Abs(angle);
            if (degrees <= 90)
            {
                double radians = 0.0174532925 * degrees;
                double dSin = Math.Sin(radians);
                double dCos = Math.Cos(radians);
                W = (int)(dH * dSin + dW * dCos);
                H = (int)(dW * dSin + dH * dCos);
                X = (W - image.Width) / 2;
                Y = (H - image.Height) / 2;
            }
            else
            {
                degrees -= 90;
                double radians = 0.0174532925 * degrees;
                double dSin = Math.Sin(radians);
                double dCos = Math.Cos(radians);
                W = (int)(dW * dSin + dH * dCos);
                H = (int)(dH * dSin + dW * dCos);
                X = (W - image.Width) / 2;
                Y = (H - image.Height) / 2;
            }
        }
        else
        {
            W = image.Width;
            H = image.Height;
            X = 0;
            Y = 0;
        }

        //create a new empty bitmap to hold rotated image
        Bitmap bmpRet = new Bitmap(W, H);
        bmpRet.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        //make a graphics object from the empty bitmap
        Graphics g = Graphics.FromImage(bmpRet);

        //Put the rotation point in the "center" of the image
        g.TranslateTransform(rotateAtX + X, rotateAtY + Y);

        //rotate the image
        g.RotateTransform(angle);

        //move the image back
        g.TranslateTransform(-rotateAtX - X, -rotateAtY - Y);

        //draw passed in image onto graphics object
        g.DrawImage(image, new PointF(0 + X, 0 + Y));

        return bmpRet;
    }

1 个答案:

答案 0 :(得分:0)

我认为你应该在OnPaint或OnDraw事件

中无效并绘画
private void timer_Elapsed(object sender, EventArgs e)
{
    //Graphics graphic = Graphics.FromImage(pictureBox1.Image); 
    //graphic.RotateTransform(45); 


   // this.Invoke(new MethodInvoker(delegate { RotateImage(pictureBox1.Image, 10); }));    

    pictureBox1.Invalidate();
}

在您的表单中,您应该启用此样式以获得更好的性能和平滑度

SetStyle( ControlStyles.ResizeRedraw, true );
SetStyle( ControlStyles.UserPaint, true );
SetStyle( ControlStyles.AllPaintingInWmPaint, true );
SetStyle( ControlStyles.OptimizedDoubleBuffer, true );