开发者

C# COM Interop and Application.DoEvents()

开发者 https://www.devze.com 2023-04-09 06:52 出处:网络
I found the following code and am trying to implement it in a COM module: public Bitmap GetThumbnail()

I found the following code and am trying to implement it in a COM module:

        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);
        }

However, I've discovered that the Application.DoEvents() and COM do not work together. The code works great in a C# application, but it hangs when called via COM. As you can tell, I'm trying to get a website thumbnail, and I can't seem to figure out a substitute for the DoEvents().

Any suggestions?

Thanks, Kevin

0

精彩评论

暂无评论...
验证码 换一张
取 消