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
{
///
/// Creates a new BitmapImage from the byte array.
///
/// The image data.
///
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;
}
}
///
/// Interaction logic for MainWindow.xaml
///
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);
}
}
}
}