WebBrowser 中的CreateGraphics()
WebBrowser控件打开文件后
使用CreateGraphics()绘出来的图如何才能显示在打开的文件上

解决方案 »

  1.   

    private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
          WebBrowser browser = (sender as WebBrowser);
          
          if (browser != null)
          {
            mshtml.IHTMLDocument2 document = (browser.Document.DomDocument as mshtml.IHTMLDocument2);
            if (document != null)
            {
              mshtml.IHTMLElement element = (document.body as mshtml.IHTMLElement);
              if (element != null)
              {
                IHTMLElementRender render = (element as IHTMLElementRender);
                if (render != null)
                {
                  using (Graphics graphics = this.CreateGraphics())
                  {
                    IntPtr hdcDestination = graphics.GetHdc();
                    render.DrawToDC(hdcDestination);
                    IntPtr hdcMemory = GDI32.CreateCompatibleDC(hdcDestination);
                    IntPtr bitmap = GDI32.CreateCompatibleBitmap(
                      hdcDestination,
                      browser.ClientRectangle.Width,
                      browser.ClientRectangle.Height
                      );
                    
                    if (bitmap != IntPtr.Zero)
                    {
                      IntPtr hOld = (IntPtr)GDI32.SelectObject(hdcMemory, bitmap);
                      GDI32.BitBlt(
                        hdcMemory,
                        0, 0,
                        browser.ClientRectangle.Width, browser.ClientRectangle.Height,
                        hdcDestination,
                        0, 0,
                        TernaryRasterOperations.SRCCOPY
                        );
                      GDI32.SelectObject(hdcMemory, hOld);
                      GDI32.DeleteDC(hdcMemory);
                      graphics.ReleaseHdc(hdcDestination);
                      
                      SaveThumbnail(Image.FromHbitmap(bitmap));
                    }
                  }
                }
              }
            }
          }
    } Once we have the image, it is then a simple matter create and save a thumbnail:private void SaveThumbnail(Image image)
    {
          if (image != null)
          {
            Bitmap thumbnail = new Bitmap(160, 120, PixelFormat.Format24bppRgb);
            thumbnail.SetResolution(image.HorizontalResolution, image.VerticalResolution);
            
            using (Graphics resize = Graphics.FromImage(thumbnail))
            {
              resize.InterpolationMode = InterpolationMode.HighQualityBicubic;
              resize.DrawImage(image,
                new Rectangle(0, 0, 160, 120),
                new Rectangle(0, 0, _webBrowser.ClientRectangle.Width, _webBrowser.ClientRectangle.Height),
                GraphicsUnit.Pixel);
            }
            thumbnail.Save(_file.FullName, ImageFormat.Png);
          }

      

  2.   

    请问下 mshtml 是什么类的实例?
      

  3.   

    IHTMLDocument2 个类可以直接实例化