aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.TwnTester.UI/MainWindow.xaml.cs
blob: 2777d03902be18f85bdc1ff22f5e5085255ee1d5 (plain)
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
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
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.Navigation;
using System.Windows.Shapes;
using Tango.Pulse;

namespace Tango.TwnTester.UI
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Creates a new BitmapImage from the byte array.
        /// </summary>
        /// <param name="data">The image data.</param>
        /// <returns></returns>
        public static BitmapImage ToBitmapImage(this byte[] data)
        {
            if (data == null || data.Length == 0) return null;
            var image = new BitmapImage();
            using (var mem = new MemoryStream(data))
            {
                mem.Position = 0;
                image.BeginInit();
                image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.UriSource = null;
                image.StreamSource = mem;
                image.EndInit();
            }
            image.Freeze();
            return image;
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private class ContractResolver : DefaultContractResolver
        {
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                var prop = base.CreateProperty(member, memberSerialization);

                prop.ShouldSerialize = (x) =>
                {
                    return
                    prop.PropertyName != "Thumbnail" &&
                    prop.PropertyName != "ThumbnailData" &&
                    prop.PropertyName != "Color" &&
                    prop.PropertyName != "EmbroideryFile";
                };

                return prop;
            }
        }

        public TwnFile TwnFile
        {
            get { return (TwnFile)GetValue(TwnFileProperty); }
            set { SetValue(TwnFileProperty, value); }
        }
        public static readonly DependencyProperty TwnFileProperty =
            DependencyProperty.Register("TwnFile", typeof(TwnFile), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();
        }

        private void menuOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Twine Embroidery File|*.twn";
            if (dlg.ShowDialog().Value)
            {
                Init(dlg.FileName);
            }
        }

        private void Init(String fileName)
        {
            try
            {
                TwnFile = TwnFile.FromFile(fileName);
                txtContent.Text = JsonConvert.SerializeObject(TwnFile, Formatting.Indented, new JsonSerializerSettings()
                {
                    ContractResolver = new ContractResolver(),
                    Formatting = Formatting.Indented,
                });

                var bitmap = TwnFile.ThumbnailData.ToBitmapImage();
                imgThumbnail.Source = bitmap;
                txtThumbnailResolution.Text = bitmap.PixelWidth + "x" + bitmap.PixelHeight;

                menuExport.IsEnabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading the specified file.\n{ex.Message}");
            }
        }

        private void menuExportEmbroideryFile_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Export Embroidery File";
            dlg.Filter = $"Embroidery Files|*.{TwnFile.EmbroideryFileFormat.ToLower()}";
            dlg.FileName = TwnFile.Name;
            dlg.DefaultExt = "." + TwnFile.EmbroideryFileFormat.ToLower();
            if (dlg.ShowDialog().Value)
            {
                File.WriteAllBytes(dlg.FileName, TwnFile.EmbroideryFile);
            }
        }

        private void menuExportThumbnailImage_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Export Thumbnail Image";
            dlg.Filter = $"Image Files|*.png";
            dlg.FileName = TwnFile.Name;
            dlg.DefaultExt = ".png";
            if (dlg.ShowDialog().Value)
            {
                File.WriteAllBytes(dlg.FileName, TwnFile.ThumbnailData);
            }
        }
    }
}