json을 사용하기 위해서는 먼저 아래 dll파일을 다운받아야 한다.
(다운로드 : https://sourceforge.net/projects/csjson/)
다운 후 System.Net.Json.dll 파일을 프로젝트 폴더 안에 넣어준다.
그리고 솔루션탐색기에서 참조탭을 오른쪽마우스 누른 후 참조추가를 실행한다.
찾아보기로 들어가서 찾아보기를 누른 후 폴더에 넣은 dll파일을 선택하고 확인하면 참조에 추가된다.
이후 System.Net.Json을 using 해주면 사용할 수 있게된다.
json 저장
이후 아래와 같이 사용하면된다. 주석은 참고하라고 남겨둔 것.
배열을 사용하고싶을 경우 아래처럼 JsonArrayCollection을 사용하면 된다.
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 | private void button_jsonsave_Click(object sender, RoutedEventArgs e) { JsonObjectCollection root = new JsonObjectCollection(); JsonArrayCollection arr = new JsonArrayCollection("Images"); int count = canvas.Children.Count; for (int i = 0; i < count; i++) { // 이미지가 아니면 널이 들어옴 Image im = canvas.Children[i] as Image; JsonObjectCollection imageee; if (im != null) { string fName = im.Tag.ToString(); imageee = new JsonObjectCollection(); imageee.Add(new JsonStringValue("FileName", fName)); imageee.Add(new JsonNumericValue("X", im.Margin.Left)); imageee.Add(new JsonNumericValue("Y", im.Margin.Top)); arr.Add(imageee); } } //JsonObjectCollection image = new JsonObjectCollection(); //image.Add(new JsonStringValue("FileName", "dd.png")); //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", "dd.png")); //image.Add(new JsonNumericValue("X", 150.0)); //image.Add(new JsonNumericValue("Y", 250.0)); //arr.Add(image); root.Add(arr); String s = root.ToString(); System.IO.File.WriteAllText("ImageData.json", s); } | cs |
이미지를 찾아 파일주소와 X,Y 좌표를 받아서 저장하였다.
이후 root에 전부 넣어준후 root를 string화하여 json파일로 저장하였다.
프로젝트풀더/bin/Debug로 가면 저장된 ImageData.json 파일이 있을 것이다.
열어보면 아래와 같이 저장되있다.
json 불러오기
이번에는 반대로 ImageData.json 파일을 읽어와 String에 저장 후
JsonTextParser를 이용해 String을 쪼개 데이터를 구분한다.
파일 주소와 X, Y좌표를 불러와 이미지를 해당 좌표로 불러준다.
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 | private void button_jsonload_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; if (File.Exists(fileName)) { Stream imageStreamSource = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; myImage = new Image(); myImage.Source = bitmapSource; myImage.Width = 200; myImage.Tag = System.IO.Path.GetFullPath(fileName); myImage.Margin = new Thickness(x, y, 0, 0); canvas.Children.Add(myImage); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } } | cs |
load 버튼을 누르면 json 파일에서 이미지주소와 좌표를 읽어 제 자리에 정상적으로 출력된다.
'프로그래밍 공부 > WPF' 카테고리의 다른 글
웹소켓 사용하기 - 1.서버 설정 (0) | 2018.06.05 |
---|---|
UI 스레드 - 다른 스레드에서 UI의 값을 바꿀때 (0) | 2018.06.05 |
파일 불러오기, 이미지 출력 및 이동하기 (0) | 2018.04.02 |
캔버스 만들기, 선, 네모, 원 그리기 (그림판) (0) | 2018.04.02 |
음악 재생하기 (MediaPlayer 사용) (1) | 2018.04.02 |