|
@@ -8,6 +8,8 @@ using System.Collections.Generic;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.IO.Compression;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows;
|
|
using System.Windows;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Media.Imaging;
|
|
@@ -16,8 +18,8 @@ namespace PixiEditor.Models.IO
|
|
{
|
|
{
|
|
public class Exporter
|
|
public class Exporter
|
|
{
|
|
{
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ static ImageFormat[] _formats = new[] { ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Tiff };
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Saves document as .pixi file that contains all document data.
|
|
/// Saves document as .pixi file that contains all document data.
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -25,10 +27,11 @@ namespace PixiEditor.Models.IO
|
|
/// <param name="path">Path where file was saved.</param>
|
|
/// <param name="path">Path where file was saved.</param>
|
|
public static bool SaveAsEditableFileWithDialog(Document document, out string path)
|
|
public static bool SaveAsEditableFileWithDialog(Document document, out string path)
|
|
{
|
|
{
|
|
|
|
+ var pixi = GetFormattedString("PixiEditor File", Constants.NativeExtensionNoDot);
|
|
SaveFileDialog dialog = new SaveFileDialog
|
|
SaveFileDialog dialog = new SaveFileDialog
|
|
{
|
|
{
|
|
- Filter = "PixiEditor Files | *.pixi",
|
|
|
|
- DefaultExt = "pixi"
|
|
|
|
|
|
+ Filter = pixi + "|" + BuildFilter(),
|
|
|
|
+ FilterIndex = 0
|
|
};
|
|
};
|
|
if ((bool)dialog.ShowDialog())
|
|
if ((bool)dialog.ShowDialog())
|
|
{
|
|
{
|
|
@@ -40,6 +43,23 @@ namespace PixiEditor.Models.IO
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static string BuildFilter()
|
|
|
|
+ {
|
|
|
|
+ var filter = string.Join("|", Formats.Select(i => GetFormattedString(i)));
|
|
|
|
+ return filter;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static string GetFormattedString(ImageFormat imageFormat)
|
|
|
|
+ {
|
|
|
|
+ var formatLower = imageFormat.ToString().ToLower();
|
|
|
|
+ return GetFormattedString(imageFormat.ToString() + " Image", formatLower);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static string GetFormattedString(string imageFormat, string formatLower)
|
|
|
|
+ {
|
|
|
|
+ return $"{imageFormat}|*.{formatLower}";
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Saves editable file to chosen path and returns it.
|
|
/// Saves editable file to chosen path and returns it.
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -48,14 +68,34 @@ namespace PixiEditor.Models.IO
|
|
/// <returns>Path.</returns>
|
|
/// <returns>Path.</returns>
|
|
public static string SaveAsEditableFile(Document document, string path)
|
|
public static string SaveAsEditableFile(Document document, string path)
|
|
{
|
|
{
|
|
- Parser.PixiParser.Serialize(ParserHelpers.ToSerializable(document), path);
|
|
|
|
|
|
+ if (Path.GetExtension(path) != Constants.NativeExtension)
|
|
|
|
+ {
|
|
|
|
+ var chosenFormat = ParseImageFormat(Path.GetExtension(path));
|
|
|
|
+ var bitmap = document.Renderer.FinalBitmap;
|
|
|
|
+ SaveAs(encodersFactory[chosenFormat](), path, bitmap.PixelWidth, bitmap.PixelHeight, bitmap);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Parser.PixiParser.Serialize(ParserHelpers.ToSerializable(document), path);
|
|
|
|
+ }
|
|
|
|
+
|
|
return path;
|
|
return path;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static ImageFormat ParseImageFormat(string fileExtension)
|
|
|
|
+ {
|
|
|
|
+ fileExtension = fileExtension.Replace(".", "");
|
|
|
|
+ return (ImageFormat)typeof(ImageFormat)
|
|
|
|
+ .GetProperty(fileExtension, BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase)
|
|
|
|
+ .GetValue(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
//static Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>> encoders = new Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>>();
|
|
//static Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>> encoders = new Dictionary<ImageFormat, Action<ExportFileDialog, WriteableBitmap>>();
|
|
//TODO remove static methods/members
|
|
//TODO remove static methods/members
|
|
static Dictionary<ImageFormat, Func<BitmapEncoder>> encodersFactory = new Dictionary<ImageFormat, Func<BitmapEncoder>>();
|
|
static Dictionary<ImageFormat, Func<BitmapEncoder>> encodersFactory = new Dictionary<ImageFormat, Func<BitmapEncoder>>();
|
|
|
|
|
|
|
|
+ public static ImageFormat[] Formats { get => _formats; }
|
|
|
|
+
|
|
static Exporter()
|
|
static Exporter()
|
|
{
|
|
{
|
|
encodersFactory[ImageFormat.Png] = () => { return new PngBitmapEncoder(); };
|
|
encodersFactory[ImageFormat.Png] = () => { return new PngBitmapEncoder(); };
|