【问题标题】:rotating a picture continuously on a windows form在 Windows 窗体上连续旋转图片
【发布时间】:2012-05-04 06:55:56
【问题描述】:

我正在尝试使用计时器类和代码项目的组合在图片框控件(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;
    }

【问题讨论】:

  • 可能您需要将返回的位图重新赋值给 pictureBox1.Image 属性以查看更改?
  • 这可能是问题所在,我做的是pictureBox1.Image = bmpRet;在方法中,但它说对象引用有问题
  • 没关系,我修好了谢谢!
  • 好吧,我让它工作了,但现在的问题是图像随着时间的推移变得越来越模糊,直到你甚至无法分辨它是什么。

标签: c# visual-studio-2010 picturebox


【解决方案1】:

我认为你应该在你的 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 );            

【讨论】:

  • 谢谢,这有帮助。但我不敢相信我忘记将返回的位图图像重新分配给图片框的图像!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多