VioletaBabel

19일 : 캔버스에 도형 그리기 본문

BCA/3. C# WPF
19일 : 캔버스에 도형 그리기
Beabletoet 2018. 3. 13. 14:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Window x:Class="WpfApp2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300"
        MouseLeftButtonDown="MyCanvas_MouseLeftButtonDown" MouseLeftButtonUp="MyCanvas_MouseLeftButtonUp" MouseMove="MyCanvas_MouseMove"
>
    <Grid >
        <Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="269" VerticalAlignment="Top" Width="292"/>
 
    </Grid>
</Window>
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace WpfApp2
{
    /// <summary>
    /// Window1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
 
        }
        private bool mClicked = false;
        private bool wasClicked = false;
        private Point prePosition = new Point(00);
        private Point nowPosition = new Point(0,0);
        private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mClicked = true;
            prePosition = e.GetPosition(MyCanvas);//MouseButtonEventArgs인 e에서 캔버스 위의 포지션을 가져옴
        }
        private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            mClicked = false;
            
            /*if(wasClicked)//타원만드는코드
            {
                wasClicked = false;
                double w = (prePosition.X > nowPosition.X) ? prePosition.X - nowPosition.X : nowPosition.X - prePosition.X;
                double h = (prePosition.Y > nowPosition.Y) ? prePosition.Y - nowPosition.Y : nowPosition.Y - prePosition.Y;
                Ellipse myEllipse = new Ellipse();
                myEllipse.StrokeThickness = 2;
                myEllipse.Stroke = Brushes.Violet;
                myEllipse.Width = w;
                myEllipse.Height = h;
                double top, left;
                top = (prePosition.Y > nowPosition.Y) ? nowPosition.Y : prePosition.Y;
                left = (prePosition.X > nowPosition.X) ? nowPosition.X : prePosition.X;
                myEllipse.Margin = new Thickness(left, top,0,0);
                MyCanvas.Children.Add(myEllipse);
            }*/
            if (wasClicked)
            {//직사각형 그리는 코드
                wasClicked = false;
                double w = (prePosition.X > nowPosition.X) ? prePosition.X - nowPosition.X : nowPosition.X - prePosition.X;
                double h = (prePosition.Y > nowPosition.Y) ? prePosition.Y - nowPosition.Y : nowPosition.Y - prePosition.Y;
                Rectangle myRect = new Rectangle();
                myRect.StrokeThickness = 2;
                myRect.Stroke = Brushes.Violet;
                myRect.Width = w;
                myRect.Height = h;
                double top, left;
                top = (prePosition.Y > nowPosition.Y) ? nowPosition.Y : prePosition.Y;
                left = (prePosition.X > nowPosition.X) ? nowPosition.X : prePosition.X;
                myRect.Margin = new Thickness(left, top, 00);
                MyCanvas.Children.Add(myRect);
            }            
        }
 
        private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            /*if(mClicked == true)
            {
                var nowPosition = e.GetPosition(MyCanvas);
                Line line = new Line();
                line.X1 = prePosition.X;//시작점
                line.Y1 = prePosition.Y;
                line.X2 = nowPosition.X;//끝점
                line.Y2 = nowPosition.Y;
                line.Stroke = System.Windows.Media.Brushes.Violet;//색
                line.StrokeThickness = 2;//굵기
                MyCanvas.Children.Add(line);
                prePosition = nowPosition;
            }*/ // 그림판처럼 마음껏 그림 그리는 코드
            if(mClicked == true)
            {//타원, 직사각형만들때 쓰는 코드
                wasClicked = true;
                nowPosition = e.GetPosition(MyCanvas);
            }
 
        }
 
    }
}
 
cs


===

이미지 띄우고 클릭&드래그로 옮기기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace WpfApp2
{
    /// <summary>
    /// Window1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Window1 : Window
    {
        //////이미지를 배경에 넣는 코드
        Image myImage = new Image();
        BitmapImage myBitmapImage = new BitmapImage();
        //////여기까지랑 밑에 또 있음.
 
        private bool mClicked = false;
        private Point prePosition = new Point(00);
        private Point nowPosition = new Point(00);
        private Point imageLeftTop = new Point(00);
        private Point imageRightBottom, clickPosition;
        private Point imageCenter;
        public Window1()
        {
            InitializeComponent();
 
            //////이미지를 배경에 넣는 코드
            myImage.Width = 261;
            myImage.Height = 193;
            // BitmapImage.UriSource must be in a BeginInit/EndInit block
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(@"C:/Users/user/source/repos/WpfApp2/WpfApp2/bin/Debug/moomin.jpg");
            // To save significant application memory, set the DecodePixelWidth or  
            // DecodePixelHeight of the BitmapImage value of the image source to the desired 
            // height or width of the rendered image. If you don't do this, the application will 
            // cache the image as though it were rendered as its normal size rather then just 
            // the size that is displayed.
            // Note: In order to preserve aspect ratio, set DecodePixelWidth
            // or DecodePixelHeight but not both.
            myBitmapImage.DecodePixelWidth = 261;
            myBitmapImage.DecodePixelHeight = 193;
            myBitmapImage.EndInit();
            //set image source
            myImage.Source = myBitmapImage;
            MyCanvas.Children.Add(myImage);
            //////여기까지
            imageRightBottom = new Point(myImage.Width, myImage.Height);
            imageCenter = new Point(myImage.Width / 2, myImage.Height / 2);
        }
        private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            prePosition = e.GetPosition(MyCanvas);//MouseButtonEventArgs인 e에서 캔버스 위의 포지션을 가져옴
            if (prePosition.X >= imageLeftTop.X && prePosition.X <= imageRightBottom.X && prePosition.Y >= imageLeftTop.Y && prePosition.Y <= imageRightBottom.Y)
                mClicked = true;
        }
        private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            mClicked = false;
            double left = imageCenter.X - myImage.Width / 2,
                right = imageCenter.X + myImage.Width / 2,
                top = imageCenter.Y - myImage.Height / 2,
                bottom = imageCenter.Y + myImage.Height / 2;
            imageRightBottom = new Point(right, bottom);
            imageLeftTop = new Point(left, top);
            
        }
 
        private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (mClicked)
            {
                nowPosition = e.GetPosition(MyCanvas);
                imageCenter.X += nowPosition.X - prePosition.X;
                imageCenter.Y += nowPosition.Y - prePosition.Y;
                double left = imageCenter.X - (myImage.Width / 2), top = imageCenter.Y - (myImage.Height / 2);
                myImage.Margin = new Thickness(left, top, 00);
                prePosition = nowPosition;
            }
        }
 
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<Window x:Class="WpfApp2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300"
        MouseMove="MyCanvas_MouseMove"
        MouseLeftButtonDown="MyCanvas_MouseLeftButtonDown" MouseLeftButtonUp="MyCanvas_MouseLeftButtonUp">
    <Grid >
        <Canvas x:Name="MyCanvas" HorizontalAlignment="Left" 
                Height="269" VerticalAlignment="Top" 
                Width="292"/>
        
    </Grid>
</Window>
 
cs



==

이미지를 파일 오픈으로 여러개 선택해 불러와, 각각 이동


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Window x:Class="WpfApp2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Window1" Height="700" Width="700"
        MouseMove="MyCanvas_MouseMove"
        MouseLeftButtonDown="MyCanvas_MouseLeftButtonDown" MouseLeftButtonUp="MyCanvas_MouseLeftButtonUp">
    <Grid >
        <Canvas x:Name="MyCanvas" HorizontalAlignment="Left" 
                Height="648" VerticalAlignment="Top" 
                Width="692" Margin="0,21,0,0"/>
        <Button Content="Load Image" HorizontalAlignment="Left" VerticalAlignment="Top" Width="692" Click="Button_Click"/>
 
    </Grid>
</Window>
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
 
namespace WpfApp2
{
    /// <summary>
    /// Window1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Window1 : Window
    {
        Image myImage;
        BitmapImage myBitmapImage;
        private bool mClicked = false;
        private bool hasImage = false;
        private Point prePosition = new Point(00);
        private Point nowPosition = new Point(00);
        private Point imageLeftTop = new Point(00);
        private Point imageRightBottom;
        private Point imageCenter;
        private string filename;
        public Window1()
        {
            InitializeComponent();
 
            
        }
        private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (hasImage)
            {
                prePosition = e.GetPosition(MyCanvas);
                foreach (Image im in MyCanvas.Children)
                {
                    double left = im.Margin.Left;
                    double top = im.Margin.Top;
                    double right = left + im.Width;
                    double bottom = top + im.Height;
                    if (prePosition.X >= left && prePosition.X <= right && prePosition.Y >= top && prePosition.Y <= bottom)
                    {
                        mClicked = true;
                        myImage = im;
                        imageRightBottom = new Point(right, bottom);
                        imageLeftTop = new Point(left, top);
                        imageCenter = new Point((left + right) / 2.0f, (top + bottom) / 2.0f);
                        break;
                    }
                }
            }
        }
        private void MyCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (hasImage)
            {
                mClicked = false;
                double left = imageCenter.X - myImage.Width / 2,
                    right = imageCenter.X + myImage.Width / 2,
                    top = imageCenter.Y - myImage.Height / 2,
                    bottom = imageCenter.Y + myImage.Height / 2;
                imageRightBottom = new Point(right, bottom);
                imageLeftTop = new Point(left, top);
            }
        }
 
        private void MyCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (mClicked)
            {
                nowPosition = e.GetPosition(MyCanvas);
                imageCenter.X += nowPosition.X - prePosition.X;
                imageCenter.Y += nowPosition.Y - prePosition.Y;
                double left = imageCenter.X - (myImage.Width / 2), top = imageCenter.Y - (myImage.Height / 2);
                myImage.Margin = new Thickness(left, top, 00);
                prePosition = nowPosition;
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myImage = new Image();
            myBitmapImage = new BitmapImage();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "*.jpg|*.jpg|*.png|*.png|*.gif|*.gif";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                filename = dlg.FileName;
                myImage.Width = 300;
                myImage.Height = 300;
                myBitmapImage.BeginInit();
                myBitmapImage.UriSource = new Uri(filename);
                myBitmapImage.DecodePixelWidth = 300;
                myBitmapImage.DecodePixelHeight = 300;
                myBitmapImage.EndInit();
                myImage.Source = myBitmapImage;
                MyCanvas.Children.Add(myImage);
                imageRightBottom = new Point(myImage.Width, myImage.Height);
                imageCenter = new Point(myImage.Width / 2, myImage.Height / 2);
                if (!hasImage)
                    hasImage = true;
            }
        }
    }
}
 
cs


'BCA > 3. C# WPF' 카테고리의 다른 글

33. 타워디펜스에 사용한, 간단한 길 찍는 툴  (0) 2018.05.03
30. 연출용 툴  (0) 2018.04.27
20일 : system.net.json  (0) 2018.03.15
18일 : mp3 플레이어  (0) 2018.03.12
17일 : C# 윈도우 폼 입문, 계산기  (0) 2018.03.09
Comments