VioletaBabel
20일 : system.net.json 본문
https://sourceforge.net/projects/csjson/?source=typ_redirect
여기에 있는 압축파일의 dll 파일을 소스코드가 있는 곳에 넣는다.
다음 솔루션 탐색기에서 참조를 오른쪽 클릭해 참조 추가로 해당 dll 파일을 넣어주고
using System.Net.Json;
을 넣어준 후 save, load 버튼을 만든다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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="110" Click="Button_Click" Height="21"/> <Button x:Name="Save" Content="Save" HorizontalAlignment="Left" Margin="110,0,0,0" Width="75" Height="21" VerticalAlignment="Top" Click="Save_Click_1"/> <Button x:Name="Load" Content="Load" HorizontalAlignment="Left" Margin="185,0,0,0" VerticalAlignment="Top" Width="75" Height="21" Click="Load_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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | 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; using System.Net.Json; namespace WpfApp2 { /// <summary> /// Window1.xaml에 대한 상호 작용 논리 /// </summary> public partial class Window1 : Window { //////이미지를 배경에 넣는 코드 Image myImage; BitmapImage myBitmapImage; //////여기까지랑 밑에 또 있음. /* foreach(image im in MyCanvas.Children) { } */ private bool mClicked = false; private bool hasImage = false; private Point prePosition = new Point(0, 0); private Point nowPosition = new Point(0, 0); private Point imageLeftTop = new Point(0, 0); 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); //MouseButtonEventArgs인 e에서 캔버스 위의 포지션을 가져옴 /* if (prePosition.X >= imageLeftTop.X && prePosition.X <= imageRightBottom.X && prePosition.Y >= imageLeftTop.Y && prePosition.Y <= imageRightBottom.Y) mClicked = true; */ 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, 0, 0); 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; // BitmapImage.UriSource must be in a BeginInit/EndInit block myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(filename); // 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 = 300; myBitmapImage.DecodePixelHeight = 300; 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); if (!hasImage) hasImage = true; } } private void Load_Click(object sender, RoutedEventArgs e) { try { String s = System.IO.File.ReadAllText("ImageData.json"); JsonTextParser textParser = new JsonTextParser(); JsonObjectCollection root = textParser.Parse(s) as JsonObjectCollection; JsonArrayCollection arr = root["Images"] as JsonArrayCollection; foreach (JsonObjectCollection obj in arr) { string fileName = (obj["FileName"] as JsonStringValue).Value; double x = (obj["X"] as JsonNumericValue).Value; double y = (obj["Y"] as JsonNumericValue).Value; myImage = new Image(); myBitmapImage = new BitmapImage(); 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); myImage.Margin = new Thickness(x, y, 0, 0); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Save_Click_1(object sender, RoutedEventArgs e) { JsonObjectCollection root = new JsonObjectCollection(); JsonArrayCollection arr = new JsonArrayCollection("Images"); //JsonObjectCollection image = new JsonObjectCollection(); //image.Add(new JsonStringValue("FileName", "moomin.jpg")); //image.Add(new JsonNumericValue("X", 50.0)); //image.Add(new JsonNumericValue("Y", 50.0)); //arr.Add(image); //image = new JsonObjectCollection(); //image.Add(new JsonStringValue("FileName", "moomin.jpg")); //image.Add(new JsonNumericValue("X", 150.0)); //image.Add(new JsonNumericValue("Y", 150.0)); //arr.Add(image); JsonObjectCollection image; foreach (Image im in MyCanvas.Children) { BitmapImage bi = myImage.Source as BitmapImage; String sss = bi.UriSource.ToString(); image = new JsonObjectCollection(); image.Add(new JsonStringValue("FileName", sss)); image.Add(new JsonNumericValue("X", im.Margin.Left)); image.Add(new JsonNumericValue("Y", im.Margin.Top)); arr.Add(image); } root.Add(arr); String s = root.ToString(); System.IO.File.WriteAllText("ImageData.json", s); } } } | 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 | <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"> <ScrollViewer HorizontalScrollBarVisibility="Visible"> <Grid > <Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="648" VerticalAlignment="Top" Width="692" Margin="0,21,0,0" MouseMove="MyCanvas_MouseMove" MouseLeftButtonDown="MyCanvas_MouseLeftButtonDown" MouseLeftButtonUp="MyCanvas_MouseLeftButtonUp"/> <Button Content="Load Image" HorizontalAlignment="Left" VerticalAlignment="Top" Width="110" Click="Button_Click" Height="21"/> <Button x:Name="Save" Content="Save" HorizontalAlignment="Left" Margin="110,0,0,0" Width="75" Height="21" VerticalAlignment="Top" Click="Save_Click_1"/> <Button x:Name="Load" Content="Load" HorizontalAlignment="Left" Margin="185,0,0,0" VerticalAlignment="Top" Width="75" Height="21" Click="Load_Click"/> </Grid> </ScrollViewer> </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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | 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; using System.Net.Json; namespace WpfApp2 { /// <summary> /// Window1.xaml에 대한 상호 작용 논리 /// </summary> public partial class Window1 : Window { //////이미지를 배경에 넣는 코드 Image myImage; BitmapImage myBitmapImage; //////여기까지랑 밑에 또 있음. /* foreach(image im in MyCanvas.Children) { } */ private bool mClicked = false; private bool hasImage = false; private Point prePosition = new Point(0, 0); private Point nowPosition = new Point(0, 0); private Point imageLeftTop = new Point(0, 0); 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); //MouseButtonEventArgs인 e에서 캔버스 위의 포지션을 가져옴 /* if (prePosition.X >= imageLeftTop.X && prePosition.X <= imageRightBottom.X && prePosition.Y >= imageLeftTop.Y && prePosition.Y <= imageRightBottom.Y) mClicked = true; */ 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, 0, 0); prePosition = nowPosition; } if (mClicked) { if (MyCanvas.Height - 50 < e.GetPosition(MyCanvas).Y) MyCanvas.Height += 100; if (MyCanvas.Width - 50 < e.GetPosition(MyCanvas).X) MyCanvas.Width += 100; } } 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; // BitmapImage.UriSource must be in a BeginInit/EndInit block myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(filename); // 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 = 300; myBitmapImage.DecodePixelHeight = 300; 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); if (!hasImage) hasImage = true; } } private void Load_Click(object sender, RoutedEventArgs e) { try { String s = System.IO.File.ReadAllText("ImageData.json"); JsonTextParser textParser = new JsonTextParser(); JsonObjectCollection root = textParser.Parse(s) as JsonObjectCollection; JsonArrayCollection arr = root["Images"] as JsonArrayCollection; foreach (JsonObjectCollection obj in arr) { string fileName = (obj["FileName"] as JsonStringValue).Value; double x = (obj["X"] as JsonNumericValue).Value; double y = (obj["Y"] as JsonNumericValue).Value; myImage = new Image(); myBitmapImage = new BitmapImage(); 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); myImage.Margin = new Thickness(x, y, 0, 0); if (!hasImage) hasImage = true; } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Save_Click_1(object sender, RoutedEventArgs e) { JsonObjectCollection root = new JsonObjectCollection(); JsonArrayCollection arr = new JsonArrayCollection("Images"); //JsonObjectCollection image = new JsonObjectCollection(); //image.Add(new JsonStringValue("FileName", "moomin.jpg")); //image.Add(new JsonNumericValue("X", 50.0)); //image.Add(new JsonNumericValue("Y", 50.0)); //arr.Add(image); //image = new JsonObjectCollection(); //image.Add(new JsonStringValue("FileName", "moomin.jpg")); //image.Add(new JsonNumericValue("X", 150.0)); //image.Add(new JsonNumericValue("Y", 150.0)); //arr.Add(image); JsonObjectCollection image; foreach (Image im in MyCanvas.Children) { BitmapImage bi = im.Source as BitmapImage; String sss = bi.UriSource.ToString(); image = new JsonObjectCollection(); image.Add(new JsonStringValue("FileName", sss)); image.Add(new JsonNumericValue("X", im.Margin.Left)); image.Add(new JsonNumericValue("Y", im.Margin.Top)); arr.Add(image); } root.Add(arr); String s = root.ToString(); System.IO.File.WriteAllText("ImageData.json", s); } } } | cs |
==
listbox : 리스트가 나옴
combobox = dropbox처럼 눌러서 밑으로 쭉 내려오고 선택하는게 뜸
'BCA > 3. C# WPF' 카테고리의 다른 글
33. 타워디펜스에 사용한, 간단한 길 찍는 툴 (0) | 2018.05.03 |
---|---|
30. 연출용 툴 (0) | 2018.04.27 |
19일 : 캔버스에 도형 그리기 (0) | 2018.03.13 |
18일 : mp3 플레이어 (0) | 2018.03.12 |
17일 : C# 윈도우 폼 입문, 계산기 (0) | 2018.03.09 |
Comments