aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/FrameworkElementExtensions.cs
blob: fae9d7821805be459fe5fd7d347eb92e19978815 (plain)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword 
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;
    }
}