2012-05-03 79 views
0

我想在图片框控件(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; 
    } 
+0

难道那你可能需要将返回的位图重新映射到pictureBox1.Image属性才能看到更改? – 2012-05-03 23:04:29

+0

这可能是问题,我做pictureBox1.Image = bmpRet;在方法,但它说,它有一个对象参考 – user1197993

+0

没有问题,我解决它谢谢你! – user1197993

回答

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);    
+0

谢谢,这有帮助。但我不能相信我忘了将返回的位图图像重新分配给图片框的图像! – user1197993