using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
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.Shapes;
namespace Tango.SharedUI.Rendering
{
///
/// Represents an on window chart renderer.
///
public partial class RenderWindow : Window
{
private Action _fileCallback;
private Action _bitmapCallback;
private Size _renderSize;
private ImageFormat _format;
private int _quality;
private String _filePath;
private FrameworkElement _element;
private bool renderToFile;
///
/// Initializes a new instance of the class.
///
/// The element.
public RenderWindow(FrameworkElement element)
{
InitializeComponent();
element.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
element.VerticalAlignment = System.Windows.VerticalAlignment.Top;
_element = element;
grid.Children.Add(_element);
}
///
/// Occurs when the window is completely loaded.
///
/// The sender.
/// The instance containing the event data.
private void RenderWindow_ContentRendered(object sender, EventArgs e)
{
RenderTargetBitmap bmp = CreateBitmap(grid, new Size(_renderSize.Width, _renderSize.Height));
if (renderToFile)
{
using (FileStream fs = new FileStream(_filePath, FileMode.Create))
{
BitmapEncoder encoder = GetEncoderByImageFormat(_format, _quality);
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
}
if (_fileCallback != null)
{
_fileCallback(_filePath);
}
}
else
{
if (_bitmapCallback != null)
{
_bitmapCallback(bmp);
}
}
this.Close();
}
///
/// Renders to file.
///
/// Size of the design.
/// Size of the render.
/// The file path.
/// The format.
/// The ready callback.
public void RenderToFile(Size designSize, Size renderSize, String filePath, ImageFormat format, Action readyCallback = null)
{
renderToFile = true;
_renderSize = renderSize;
_filePath = filePath;
_format = format;
_fileCallback = readyCallback;
double scaleX = renderSize.Width / designSize.Width;
double scaleY = scaleX;
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = true;
this.Opacity = 0.001;
this.ShowInTaskbar = false;
this.ShowActivated = false;
this.Width = 1;
this.Height = 1;
_element.Width = designSize.Width;
_element.Height = designSize.Height;
grid.LayoutTransform = new ScaleTransform(scaleX, scaleY, 0, 0);
this.ContentRendered += RenderWindow_ContentRendered;
this.Show();
}
///
/// Renders to file.
///
/// Size of the design.
/// Size of the render.
/// The file path.
/// The format.
/// The ready callback.
public void RenderToFileSynced(Size designSize, Size renderSize, String filePath, ImageFormat format, int quality, Action readyCallback = null)
{
renderToFile = true;
_renderSize = renderSize;
_filePath = filePath;
_format = format;
_quality = quality;
_fileCallback = readyCallback;
double scaleX = renderSize.Width / designSize.Width;
double scaleY = scaleX;
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = true;
this.Opacity = 0.001;
this.ShowInTaskbar = false;
this.ShowActivated = false;
this.Width = 1;
this.Height = 1;
_element.Width = designSize.Width;
_element.Height = designSize.Height;
grid.LayoutTransform = new ScaleTransform(scaleX, scaleY, 0, 0);
this.ContentRendered += RenderWindow_ContentRendered;
this.ShowDialog();
}
///
/// Renders to bitmap.
///
/// Size of the design.
/// Size of the render.
/// The ready callback.
public void RenderToBitmap(Size designSize, Size renderSize, Action readyCallback = null)
{
_renderSize = renderSize;
_bitmapCallback = readyCallback;
double scaleX = renderSize.Width / designSize.Width;
double scaleY = scaleX;
this.WindowStyle = WindowStyle.None;
this.AllowsTransparency = true;
this.Opacity = 0.001;
this.ShowInTaskbar = false;
this.ShowActivated = false;
this.Width = 1;
this.Height = 1;
_element.Width = designSize.Width;
_element.Height = designSize.Height;
grid.LayoutTransform = new ScaleTransform(scaleX, scaleY, 0, 0);
this.ContentRendered += RenderWindow_ContentRendered;
this.Show();
}
///
/// Creates the bitmap.
///
/// The o visual.
/// Size of the o.
///
private RenderTargetBitmap CreateBitmap(Visual oVisual, Size oSize)
{
int nWidth = (int)Math.Ceiling(oSize.Width);
int nHeight = (int)Math.Ceiling(oSize.Height);
RenderTargetBitmap oTargetBitmap = new RenderTargetBitmap(
nWidth,
nHeight,
96,
96,
PixelFormats.Pbgra32
);
DrawingVisual oDrawingVisual = new DrawingVisual();
using (DrawingContext oDrawingContext = oDrawingVisual.RenderOpen())
{
VisualBrush oVisualBrush = new VisualBrush(oVisual) { Stretch = Stretch.Fill };
oDrawingContext.DrawRectangle(
oVisualBrush,
null,
new Rect(
new Point(),
new Size(nWidth, nHeight)
)
);
oDrawingContext.Close();
oTargetBitmap.Render(oDrawingVisual);
}
return oTargetBitmap;
}
///
/// Gets the encoder by image format.
///
/// The format.
///
private static BitmapEncoder GetEncoderByImageFormat(ImageFormat format, int quality)
{
BitmapEncoder encoder = null;
if (format == ImageFormat.Png)
{
encoder = new PngBitmapEncoder();
}
else if (format == ImageFormat.Jpeg)
{
encoder = new JpegBitmapEncoder();
(encoder as JpegBitmapEncoder).QualityLevel = quality;
}
else
{
encoder = new BmpBitmapEncoder();
}
return encoder;
}
}
}