-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper.cs
73 lines (64 loc) · 2.24 KB
/
Helper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace videomaker
{
public class Helper
{
public static System.Drawing.Bitmap ConvertWindowToBitmap(Window window, int dpi)
{
var rtb = new RenderTargetBitmap(
(int)window.Width, //width
(int)window.Width, //height
dpi, //dpi x
dpi, //dpi y
PixelFormats.Pbgra32 // pixelformat
);
rtb.Render(window);
var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
BitmapFrame frame = System.Windows.Media.Imaging.BitmapFrame.Create(rtb);
return null;
}
public static void SaveWindow(Window window, int dpi, string filename)
{
var rtb = new RenderTargetBitmap(
(int)window.Width, //width
(int)window.Width, //height
dpi, //dpi x
dpi, //dpi y
PixelFormats.Pbgra32 // pixelformat
);
rtb.Render(window);
SaveRTBAsPNG(rtb, filename);
}
public static void SaveCanvas(Window window, Canvas canvas, int dpi, string filename)
{
Size size = new Size(window.Width, window.Height);
canvas.Measure(size);
//canvas.Arrange(new Rect(size));
var rtb = new RenderTargetBitmap(
(int)window.Width, //width
(int)window.Height, //height
dpi, //dpi x
dpi, //dpi y
PixelFormats.Pbgra32 // pixelformat
);
rtb.Render(canvas);
SaveRTBAsPNG(rtb, filename);
}
private static void SaveRTBAsPNG(RenderTargetBitmap bmp, string filename)
{
var enc = new System.Windows.Media.Imaging.PngBitmapEncoder();
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
using (var stm = System.IO.File.Create(filename))
{
enc.Save(stm);
}
}
}
}