-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathGenerateImageForm.cs
More file actions
153 lines (134 loc) · 5.63 KB
/
GenerateImageForm.cs
File metadata and controls
153 lines (134 loc) · 5.63 KB
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
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Reflection;
using System.Windows.Forms;
using TheArtOfDev.HtmlRenderer.Core;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.Demo.Common;
using TheArtOfDev.HtmlRenderer.WinForms;
namespace TheArtOfDev.HtmlRenderer.Demo.WinForms
{
public partial class GenerateImageForm : Form
{
private readonly string _html;
private readonly Bitmap _background;
public GenerateImageForm(string html)
{
_html = html;
InitializeComponent();
Icon = DemoForm.GetIcon();
_background = HtmlRenderingHelper.CreateImageForTransparentBackground();
foreach (var color in GetColors())
{
if (color != Color.Transparent)
_backgroundColorTSB.Items.Add(color.Name);
}
_backgroundColorTSB.SelectedItem = Color.White.Name;
foreach (var hint in Enum.GetNames(typeof(TextRenderingHint)))
{
_textRenderingHintTSCB.Items.Add(hint);
}
_textRenderingHintTSCB.SelectedItem = TextRenderingHint.AntiAlias.ToString();
_useGdiPlusTSB.Enabled = true;
_backgroundColorTSB.Enabled = true;
}
private void OnSaveToFile_Click(object sender, EventArgs e)
{
using (var saveDialog = new SaveFileDialog())
{
saveDialog.Filter = "Images|*.png;*.bmp;*.jpg";
saveDialog.FileName = "image";
saveDialog.DefaultExt = ".png";
var dialogResult = saveDialog.ShowDialog(this);
if (dialogResult == DialogResult.OK)
{
_pictureBox.Image.Save(saveDialog.FileName);
}
}
}
private void OnUseGdiPlus_Click(object sender, EventArgs e)
{
_useGdiPlusTSB.Checked = !_useGdiPlusTSB.Checked;
_textRenderingHintTSCB.Visible = _useGdiPlusTSB.Checked;
_backgroundColorTSB.Visible = !_useGdiPlusTSB.Checked;
_toolStripLabel.Text = _useGdiPlusTSB.Checked ? "Text Rendering Hint:" : "Background:";
GenerateImage();
}
private void OnBackgroundColor_SelectedIndexChanged(object sender, EventArgs e)
{
GenerateImage();
}
private void _textRenderingHintTSCB_SelectedIndexChanged(object sender, EventArgs e)
{
GenerateImage();
}
private void OnGenerateImage_Click(object sender, EventArgs e)
{
GenerateImage();
}
private void GenerateImage()
{
if (_backgroundColorTSB.SelectedItem != null && _textRenderingHintTSCB.SelectedItem != null)
{
var backgroundColor = Color.FromName(_backgroundColorTSB.SelectedItem.ToString());
TextRenderingHint textRenderingHint = (TextRenderingHint)Enum.Parse(typeof(TextRenderingHint), _textRenderingHintTSCB.SelectedItem.ToString());
Image img;
if (_useGdiPlusTSB.Checked)
{
img = HtmlRender.RenderToImageGdiPlus(_html, _pictureBox.ClientSize, textRenderingHint, null, DemoUtils.OnStylesheetLoad, HtmlRenderingHelper.OnImageLoad);
}
else
{
EventHandler<HtmlStylesheetLoadEventArgs> stylesheetLoad = DemoUtils.OnStylesheetLoad;
EventHandler<HtmlImageLoadEventArgs> imageLoad = HtmlRenderingHelper.OnImageLoad;
var objects = new object[] { _html, _pictureBox.ClientSize, backgroundColor, null, stylesheetLoad, imageLoad };
var types = new[] { typeof(String), typeof(Size), typeof(Color), typeof(CssData), typeof(EventHandler<HtmlStylesheetLoadEventArgs>), typeof(EventHandler<HtmlImageLoadEventArgs>) };
var m = typeof(HtmlRender).GetMethod("RenderToImage", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, types, null);
img = (Image)m.Invoke(null, objects);
}
_pictureBox.Image = img;
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
using (var b = new TextureBrush(_background, WrapMode.Tile))
{
e.Graphics.FillRectangle(b, ClientRectangle);
}
}
private static List<Color> GetColors()
{
const MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
PropertyInfo[] properties = typeof(Color).GetProperties();
List<Color> list = new List<Color>();
for (int i = 0; i < properties.Length; i++)
{
PropertyInfo info = properties[i];
if (info.PropertyType == typeof(Color))
{
MethodInfo getMethod = info.GetGetMethod();
if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
{
list.Add((Color)info.GetValue(null, null));
}
}
}
return list;
}
}
}