62,041
社区成员
发帖
与我相关
我的任务
分享
internal class WebPreviewBase : IDisposable
{
// Fields
private bool _fullpage = false;
private int _thumbH = 0x300;
private int _thumbW = 0x400;
private Uri _uri= new Uri("about:blank");
private WebBrowser _wb = new WebBrowser();
private int h;
private int w;
// 构造函数
public WebPreviewBase(Uri uri, int thumbW, int thumbH, bool fullpage)
{
this._wb.ScriptErrorsSuppressed = false;
this._wb.ScrollBarsEnabled = false;
this._wb.Size = new Size(0x400, 0x300);
this._wb.NewWindow += new CancelEventHandler(this.CancelEventHandler);
this._wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.DocCompletedEventHandler);
this._thumbW = thumbW;
this._thumbH = thumbH;
this._uri = uri;
this._fullpage = fullpage;
}
public Bitmap GetWebPreview()
{
Bitmap Bmp3;
try
{
object o = this._wb.ActiveXInstance;
this._wb.GoBack();
this.InitComobject();
Snapshot snap = new Snapshot();
Bitmap thumBitmap = (Bitmap) ImageLibrary.ResizeImageToAFixedSize(snap.TakeSnapshot(this._wb.ActiveXInstance, new Rectangle(0, 0, this.w, this.h)), this._thumbW, this._thumbH, ImageLibrary.ScaleMode.W);
Bmp3 = thumBitmap;
}
catch (Exception ex)
{
throw ex;
}
return Bmp3;
}
public void Dispose()
{
this._wb.Dispose();
}
namespace Okpower.Utility
{
public class WebPreview
{
// Fields
private Bitmap _bitmap;
private Exception _ex;
private bool _fullPage;
private int _height;
private int _timeout;
private Uri _uri;
private int _width;
// Methods
// 实现基类的构造函数
private WebPreview(Uri uri)
: this(uri, 0x7530, 200, 150, true)
{
}
//构造函数
private WebPreview(Uri uri, int timeout, int width, int height, bool fullPage)
{
this._uri = null;
this._ex = null;
this._bitmap = null;
this._timeout = 0x7530;
this._width = 200;
this._height = 150;
this._fullPage = true;
this._uri = uri;
this._timeout = timeout;
this._width = width;
this._height = height;
this._fullPage = fullPage;
}
// 启动单线程单元 获得位图
internal Bitmap GetWebPreview()
{
Thread t = new Thread(new ParameterizedThreadStart(WebPreview.StaRun));
t.SetApartmentState(ApartmentState.STA);
t.Start(this);
if (!t.Join(this._timeout))
{
t.Abort();
throw new TimeoutException();
}
if (this._ex != null)
{
throw this._ex;
}
if (this._bitmap == null)
{
throw new ExecutionEngineException();
}
return this._bitmap;
}
//
public static Bitmap GetWebPreview(Uri uri)
{
WebPreview wp = new WebPreview(uri);
return wp.GetWebPreview();
}
public static Bitmap GetWebPreview(Uri uri, int timeout, int width, int height, bool fullPage)
{
WebPreview wp = new WebPreview(uri, timeout, width, height, fullPage);
return wp.GetWebPreview();
}
private static void StaRun(object _wp)
{
WebPreview wp = (WebPreview)_wp;
try
{
wp._bitmap = new WebPreviewBase(wp._uri, wp._width, wp._height, wp._fullPage).GetWebPreview();
}
catch (Exception ex)
{
wp._ex = ex;
}
}
}