How to use.
1. Create a openDialog and set the filter to select only xaml files. assuming you
have the necessary try/catch blocks,
read it into a stream, and the rest of the code is selfexplanatory
2. 'filepath' contains the folder path to save the image.
3. List
<DependencyPropertyReplacement> replacements = null;
4. Use this code to call the ImageCreator.
using
(Stream stream = File.OpenRead(opendialog.FullName))
{
using (MemoryStream outputStream= new MemoryStream())
{
XAMLImageCreator creator = new XAMLImageCreator();
// Set the preferred height and width
creator.Convert(stream, width, height, outputStream, replacements);
}
}
using
System;
using System.IO;
using System.Windows;
using System.Reflection;
using System.Threading;
using System.Windows.Markup;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
namespace Utilities
{
public class XAMLImageCreator
{
/// <summary>
/// Converts the specified xaml input.
/// </summary>
/// <param name="xamlInput">The xaml input.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="imgOutput">The img output.</param>
/// <param name="replacements">The replacements.</param>
/// <param name="encoder">The encoder.</param>
public void Convert(Stream xamlInput, double width, double height, Stream imgOutput,
List<DependencyPropertyReplacement>
replacements, BitmapEncoder encoder)
{
Thread
imgThread = new Thread((ThreadStart)delegate()
{
FrameworkElement
element = XamlReader.Load(xamlInput) as FrameworkElement;
if (replacements != null)
{
foreach (DependencyPropertyReplacement replacement in replacements)
{
DependencyObject
replacementElement =
element.FindName(replacement.ElementName)
as DependencyObject;
if (null != replacementElement)
{
//Get the type of the current replacementElement
Type
t =
replacementElement.GetType();
//Get the field info of the current replacementElement
FieldInfo
fieldInfo = t.GetField(replacement.PropertyName,
BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
if (null != fieldInfo)
{
DependencyProperty
dp = (DependencyProperty)fieldInfo.GetValue(null);
replacementElement.SetValue(dp,
replacement.Value);
}
}
}
}
Size
renderingSize = new Size(width, height);
element.Measure(renderingSize);
Rect
renderingRectangle = new Rect(renderingSize);
element.Arrange(renderingRectangle);
BitmapSource
xamlBitmap = RenderToBitmap(element);
//Add the frame to the encoder
encoder.Frames.Add(BitmapFrame.Create(xamlBitmap));
//Used the user-defined encoder to save the output in the desired format
encoder.Save(imgOutput);
});
imgThread.IsBackground
= true;
imgThread.SetApartmentState(ApartmentState.STA);
imgThread.Start();
imgThread.Join();
}
/// <summary>
/// Renders to bitmap.
/// </summary>
/// <param name="target">The target.</param>
/// <returns></returns>
private BitmapSource RenderToBitmap(FrameworkElement target)
{
Rect
bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap
renderBitmap = new RenderTargetBitmap((int)target.ActualWidth,
(int)target.ActualHeight,
96,
96, PixelFormats.Pbgra32);
DrawingVisual
visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush
brush = new VisualBrush(target);
context.DrawRectangle(brush,
null, new Rect(new Point(), bounds.Size));
}
renderBitmap.Render(visual);
return renderBitmap;
}
}
/// <summary>
/// DependencyPropertyReplacement
/// </summary>
public class DependencyPropertyReplacement
{
/// <summary>
/// Gets or sets the name of the element.
/// </summary>
/// <value>The name of the element.</value>
public string ElementName
{
get;
set;
}
/// <summary>
///
/// </summary>
private string _propertyName;
/// <summary>
/// Gets or sets the name of the property.
/// </summary>
/// <value>The name of the property.</value>
public string PropertyName
{
get
{ return _propertyName; }
set
{
if (value == null)
{
value = "";
}
if (!value.EndsWith("Property"))
{
value += "Property";
}
_propertyName
= value;
}
}
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public object Value
{
get;
set;
}
}
}