'이미지 회전'에 해당되는 글 1건

  1. 2010.11.04 Image Rotate 4
Window/WPF2010. 11. 4. 16:11

이미지를 마우스로 회전 하는 예제입니다.

 

 

소스는 다음과 같습니다.

 

public partial class Window1 : Window

    {

        private RotateTransform rotateTransform;

        private double angle;

        private double rotate;

        private Vector position;

 

        public Window1()

        {

            InitializeComponent();

            Loaded += new RoutedEventHandler(Window1_Loaded);

        }

 

        private void Window1_Loaded(object sender, RoutedEventArgs e)

        {

            vinyl.MouseLeftButtonDown += new MouseButtonEventHandler(vinyl_MouseLeftButtonDown);

            vinyl.MouseMove += new MouseEventHandler(vinyl_MouseMove);

            vinyl.MouseLeftButtonUp += new MouseButtonEventHandler(vinyl_MouseLeftButtonUp);

 

            rotateTransform = new RotateTransform();

            vinyl.RenderTransform = rotateTransform;

            vinyl.RenderTransformOrigin = new Point(0.5, 0.5);

 

            position = VisualTreeHelper.GetOffset(vinyl);

            position.X += vinyl.Width / 2;

            position.Y += vinyl.Height / 2;   

        }

 

        private void vinyl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            Mouse.Capture(vinyl, CaptureMode.None);

        }

 

        private void vinyl_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.LeftButton == MouseButtonState.Pressed)

            {

                Point currentPoint = e.GetPosition(this);

 

                double _angle = Math.Atan2(position.Y - currentPoint.Y, position.X - currentPoint.X) *

(180 / Math.PI);

                rotateTransform.Angle = rotate + _angle - angle;

            }

        }

 

        private void vinyl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

            Mouse.Capture(vinyl, CaptureMode.Element);

            Point point = e.GetPosition(this);

           

            angle = Math.Atan2(position.Y - point.Y, position.X - point.X) * (180 / Math.PI);

            rotate = rotateTransform.Angle;

        }

    }


Posted by 열ㅇl