aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Rendering/RenderWindow.xaml.cs
blob: 638bf7267b11460d8b3116b139b1deec1e53b4f9 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
{
    /// <summary>
    /// Represents an on window chart renderer.
    /// </summary>
    public partial class RenderWindow : Window
    {
        private Action<String> _fileCallback;
        private Action<RenderTargetBitmap> _bitmapCallback;
        private Size _renderSize;
        private ImageFormat _format;
        private int _quality;
        private String _filePath;
        private FrameworkElement _element;
        private bool renderToFile;

        /// <summary>
        /// Initializes a new instance of the <see cref="RenderWindow"/> class.
        /// </summary>
        /// <param name="element">The element.</param>
        public RenderWindow(FrameworkElement element)
        {
            InitializeComponent();
            element.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            element.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            _element = element;
            grid.Children.Add(_element);
        }

        /// <summary>
        /// Occurs when the window is completely loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        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();
        }

        /// <summary>
        /// Renders to file.
        /// </summary>
        /// <param name="designSize">Size of the design.</param>
        /// <param name="renderSize">Size of the render.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="format">The format.</param>
        /// <param name="readyCallback">The ready callback.</param>
        public void RenderToFile(Size designSize, Size renderSize, String filePath, ImageFormat format, Action<String> 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();
        }

        /// <summary>
        /// Renders to file.
        /// </summary>
        /// <param name="designSize">Size of the design.</param>
        /// <param name="renderSize">Size of the render.</param>
        /// <param name="filePath">The file path.</param>
        /// <param name="format">The format.</param>
        /// <param name="readyCallback">The ready callback.</param>
        public void RenderToFileSynced(Size designSize, Size renderSize, String filePath, ImageFormat format, int quality, Action<String> 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();
        }

        /// <summary>
        /// Renders to bitmap.
        /// </summary>
        /// <param name="designSize">Size of the design.</param>
        /// <param name="renderSize">Size of the render.</param>
        /// <param name="readyCallback">The ready callback.</param>
        public void RenderToBitmap(Size designSize, Size renderSize, Action<RenderTargetBitmap> 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();
        }

        /// <summary>
        /// Creates the bitmap.
        /// </summary>
        /// <param name="oVisual">The o visual.</param>
        /// <param name="oSize">Size of the o.</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Gets the encoder by image format.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <returns></returns>
        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;
        }
    }
}