0

I used WebBrowser to create a bitmap,the application is runed in IIS web server,but the program throw a exception like this Error HRESULT E_FAIL has been returned from a call to a COM component..The variable text is a html code include JPG URL,the [url] is (http://pic.ctrip.com/mail/iata_logo.png) Anyone can help me?Thank you ! this is my code

            AutoResetEvent re = new AutoResetEvent(false);

            Thread tread = new Thread(delegate()
            {

                try
                {
                    MemoryStream mss = new MemoryStream();
                    System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
                    //wb.ScriptErrorsSuppressed = true;
                    wb.Navigate("about:blank");
                    wb.ScrollBarsEnabled = false;
                    wb.ClientSize = new Size(m_BrowserWidth, m_BrowserHeight);
                    //wb.ScrollBarsEnabled = false;

                    wb.Document.Write(text.ToString());
                    //wb.Refresh();
                    Bitmap m_Bitmap = new Bitmap(m_BrowserWidth, m_BrowserHeight);

                    //if (wb.ReadyState != WebBrowserReadyState.Complete)
                    //{
                    //    Application.DoEvents();
                    //}


                    System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(0, 0, m_BrowserWidth, m_BrowserHeight);
                    wb.DrawToBitmap(m_Bitmap, rectangle);
                    string fileName = "e:\\ddd" + Guid.NewGuid().ToString() + ".jpg";
                    m_Bitmap.Save(fileName);
                    m_Bitmap.Save(mss, System.Drawing.Imaging.ImageFormat.Jpeg);//JPG、GIF、PNG等均可  
                    buff = mss.ToArray();
                    re.Set();
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex);
                }
            });
            tread.SetApartmentState(ApartmentState.STA);
            tread.Start();
            re.WaitOne();
kyzh101
  • 101
  • 1
  • 9
  • This should help: http://stackoverflow.com/a/21828265/1768303. On a side note, a busy waiting loop with `DoEvents is never a good idea. – noseratio Jan 14 '15 at 12:25
  • 1
    Thank you very much!This is the answer I am looking for!It`s so useful for me !Warm-hearted man! – kyzh101 Jan 14 '15 at 13:05
  • @Noseratio I find that the code had registried FEATURE_BROWSER_EMULATION,FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI and FEATURE_GPU_RENDERING ,but our environment in test and production does not have the items,do you have any new suggestions can solve the problem?Thank you! – kyzh101 Jan 16 '15 at 11:07
  • These items are not there by default. Your application needs to register them, but it's done in HKCU registry hive and doesn't require admin rights. – noseratio Jan 16 '15 at 22:27
  • i have added log on the application.but it shows that my appliation does not have the access to registry them – kyzh101 Jan 18 '15 at 00:54
  • I can't tell why you're getting access denied error while writing under `HKEY_CURRENT_USER`. Perhaps, it's caused by an antivirus software or by restrictive OS policies. – noseratio Jan 19 '15 at 05:08

0 Answers0