using Tango.Editors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
///
///
/// A collection of extension methods.
///
public static class FrameworkElementExtensions
{
///
/// Renders the element to a bitmap source.
///
/// The element.
/// if set to true forces the redraw of the element and prevents a freezing effect.
///
public static BitmapSource TakeSnapshot(this FrameworkElement element, bool forceRedraw = true)
{
var bitmap = UIHelper.TakeSnapshot(element, new Size(element.ActualWidth, element.ActualHeight));
var parent = element.Parent;
if (parent != null && forceRedraw)
{
UIHelper.RemoveChild(parent, element);
element.UpdateLayout();
UIHelper.AddChild(parent, element);
element.UpdateLayout();
}
return bitmap;
}
///
/// Renders the element to a bitmap source of the specified size.
///
/// The element.
/// The bitmap width.
/// The bitmap height.
/// if set to true forces the redraw of the element and prevents a freezing effect.
///
public static BitmapSource TakeSnapshot(this FrameworkElement element, int width, int height, bool forceRedraw = true)
{
var bitmap = UIHelper.TakeSnapshot(element, new Size(element.ActualWidth, element.ActualHeight));
TransformedBitmap resizedBitmap = new TransformedBitmap(bitmap, new ScaleTransform(width / bitmap.Width, height / bitmap.Height, 0, 0));
var parent = element.Parent;
if (parent != null && forceRedraw)
{
UIHelper.RemoveChild(parent, element);
element.UpdateLayout();
UIHelper.AddChild(parent, element);
element.UpdateLayout();
}
return resizedBitmap;
}
}