1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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;
/// <exclude/>
/// <summary>
/// A collection of <see cref="FrameworkElement"/> extension methods.
/// </summary>
public static class FrameworkElementExtensions
{
/// <summary>
/// Renders the element to a bitmap source.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="forceRedraw">if set to <c>true</c> forces the redraw of the element and prevents a freezing effect.</param>
/// <returns></returns>
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;
}
/// <summary>
/// Renders the element to a bitmap source of the specified size.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="width">The bitmap width.</param>
/// <param name="height">The bitmap height.</param>
/// <param name="forceRedraw">if set to <c>true</c> forces the redraw of the element and prevents a freezing effect.</param>
/// <returns></returns>
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;
}
}
|