我找到了以下代码,并尝试在COM模块中实现它:
public Bitmap GetThumbnail()
{
ThreadStart _threadstart = new ThreadStart(GenerateThumbnail);
Thread _thread = new Thread(_threadstart);
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
_thread.Join();
return _image;
}
private void GenerateThumbnail()
{
WebBrowser _browser = new WebBrowser();
_browser.ScrollBarsEnabled = false;
_browser.ScriptErrorsSuppressed = true;
_browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_browser_DocumentCompleted);
_browser.Navigate(_url);
while (_browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
_browser.Dispose();
}
void _browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser _browser = (WebBrowser)sender;
_browser.ClientSize = new Size(this._pagewidth, this._pageheight);
_browser.ScrollBarsEnabled = false;
_image = new Bitmap(_browser.Bounds.Width, _browser.Bounds.Height);
_browser.BringToFront();
_browser.DrawToBitmap(_image, _browser.Bounds);
if (_imageheight != 0 && _imagewidth != 0)
_image = (Bitmap)_image.GetThumbnailImage(_imagewidth, _imageheight, null, IntPtr.Zero);
}
但是,我发现Application.DoEvents()和COM不能一起工作。该代码在C#应用程序中运行良好,但在通过COM调用时会挂起。你可以告诉我,我正在尝试获取一个网站缩略图,而我似乎无法找到DoEvents()的替代品。
有什么建议吗?
谢谢, 凯文