Add Export feature to visualization editor

It's nice to be able to save images from the visualization editor
for display elsewhere.  This can be done during HTML export, but
that's inconvenient when you just want one image, and doesn't allow
the output size to be specified.

This change adds an Export button to the Edit Visualization dialog.
The current bitmap, wireframe, or wireframe animation can be saved
to a GIF image.  A handful of sizes can be selected from a pop-up
menu.
This commit is contained in:
Andy McFadden committed 2020-06-20 17:32:57 -07:00
1 parent b43fd07688
commit c47beffcee
10 files changed
+312 -9

No files matched your search

+35 -1
View File
@@ -17,10 +17,10 @@ using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace CommonWPF {
/// <summary>
@@ -263,4 +263,38 @@ namespace CommonWPF {
}
}
}
/// <summary>
/// BitmapSource extensions.
/// </summary>
public static class BitmapSourceExtensions {
/// <summary>
/// Creates a scaled copy of a BitmapSource. Only scales up, using nearest-neighbor.
/// </summary>
public static BitmapSource CreateScaledCopy(this BitmapSource src, int scale) {
// Simple approach always does a "blurry" scale.
//return new TransformedBitmap(src, new ScaleTransform(scale, scale));
// Adapted from https://weblogs.asp.net/bleroy/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi
// (found via https://stackoverflow.com/a/25570225/294248)
BitmapScalingMode scalingMode = BitmapScalingMode.NearestNeighbor;
int newWidth = (int)src.Width * scale;
int newHeight = (int)src.Height * scale;
var group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, scalingMode);
group.Children.Add(new ImageDrawing(src,
new Rect(0, 0, newWidth, newHeight)));
var targetVisual = new DrawingVisual();
var targetContext = targetVisual.RenderOpen();
targetContext.DrawDrawing(group);
var target = new RenderTargetBitmap(
newWidth, newHeight, 96, 96, PixelFormats.Default);
targetContext.Close();
target.Render(targetVisual);
var targetFrame = BitmapFrame.Create(target);
return targetFrame;
}
}
}