使用值旋转PictureBox中的图像

时间:2013-08-25 18:21:17

标签: c# winforms

我正在尝试编写一个显示带有PictureBox的Wii遥控器旋转的程序。当Wii遥控器在X轴上旋转时,Wii遥控器的图像也会旋转。旋转值已显示在文本框中,如下所示:0.109243284720305-0.132414335

如何使用这些值旋转显示的图像?

1 个答案:

答案 0 :(得分:2)

创建继承自PictureBox的自定义控件,并覆盖OnPaint虚拟方法以根据需要绘制wiimote位图:

 public partial class WiiMoteControl: PictureBox {

    public Bitmap wiimote;
    private float _angle;
    public float Angle { 
         get { return _angle; } 
         set { _angle = value; Invalidate( ); } 
    }

    protected override void OnPaint( PaintEventArgs pe ) {
        base.OnPaint( pe );
        pe.Graphics.ResetTransform( );
        pe.Graphics.TranslateTransform( Size.Width / 2, Size.Height / 2 );
        pe.Graphics.RotateTransform( Angle );
        pe.Graphics.TranslateTransform( -Size.Width / 2, -Size.Height / 2 );
        if (wiimote != null) {
            pe.Graphics.DrawImage( wiimote, 0, 0, Size.Width, Size.Height );
        }
    }
}
相关问题