-
Notifications
You must be signed in to change notification settings - Fork 1
/
IEBrowser.cs
181 lines (157 loc) · 5.39 KB
/
IEBrowser.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
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
/*
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
This is sample code and is freely distributable.
*/
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Bitboxx.Web.GeneratedImage.Transform
{
public class IEBrowser : ApplicationContext
{
public Image Thumb;
private string _html;
private UrlRatioMode _ratio;
AutoResetEvent ResultEvent;
private WebBrowser _browser;
public enum UrlRatioMode
{
Full,
Screen,
Cinema
}
public IEBrowser(string target, UrlRatioMode ratio, AutoResetEvent resultEvent)
{
ResultEvent = resultEvent;
Thread thrd = new Thread(new ThreadStart(
delegate {
Init(target,ratio);
Application.Run(this);
}));
// set thread to STA state before starting
thrd.SetApartmentState(ApartmentState.STA);
thrd.Start();
}
private void Init(string target,UrlRatioMode ratio)
{
// create a WebBrowser control
_browser = new WebBrowser();
_browser.ScrollBarsEnabled = false;
_browser.ScriptErrorsSuppressed = true;
// set WebBrowser event handle
_browser.DocumentCompleted += IEBrowser_DocumentCompleted;
_ratio = ratio;
if (target.ToLower().StartsWith("http:")|| target.ToLower().StartsWith("https:"))
{
_html = "";
_browser.Navigate(target);
}
else
{
_browser.Navigate("about:blank");
_html = target;
}
}
// DocumentCompleted event handle
void IEBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string url = e.Url.ToString();
if (!(url.StartsWith("http://") || url.StartsWith("https://")))
{
// in AJAX
}
if (e.Url.AbsolutePath != _browser.Url.AbsolutePath)
{
// IFRAME
}
else
{
// REAL DOCUMENT COMPLETE
try
{
WebBrowser browser = (WebBrowser)sender;
browser.Width = 1280;
browser.Height = 1024;
HtmlDocument doc = browser.Document;
if (_html != String.Empty)
{
doc.OpenNew(true);
doc.Write(_html);
}
switch (_ratio)
{
case UrlRatioMode.Full:
browser.Width = doc.Body.ScrollRectangle.Width;
browser.Height = doc.Body.ScrollRectangle.Height;
break;
case UrlRatioMode.Screen:
browser.Width = doc.Body.ScrollRectangle.Width;
browser.Height = Convert.ToInt32(browser.Width / 3 * 2);
break;
case UrlRatioMode.Cinema:
browser.Width = doc.Body.ScrollRectangle.Width;
browser.Height = Convert.ToInt32(browser.Width / 16 * 9);
break;
default:
throw new ArgumentOutOfRangeException();
}
Bitmap bitmap = new Bitmap(browser.Width, browser.Height);
GetImage(browser.ActiveXInstance, bitmap, Color.White);
browser.Dispose();
Thumb = (Image)bitmap;
}
catch (Exception)
{
}
finally
{
ResultEvent.Set();
}
}
}
[ComImport]
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IViewObject
{
void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static void GetImage(object obj, Image destination, Color backgroundColor)
{
using (Graphics graphics = Graphics.FromImage(destination))
{
IntPtr deviceContextHandle = IntPtr.Zero;
RECT rectangle = new RECT();
rectangle.Right = destination.Width;
rectangle.Bottom = destination.Height;
graphics.Clear(backgroundColor);
try
{
deviceContextHandle = graphics.GetHdc();
IViewObject viewObject = obj as IViewObject;
viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
}
finally
{
if (deviceContextHandle != IntPtr.Zero)
{
graphics.ReleaseHdc(deviceContextHandle);
}
}
}
}
}
}