就是这个问题,我查了下,好象WebBrowser打印是可以的,我想有方法打印的到图片文件不是可以解决了,无奈没有参考好的参考代码。不知还有没有其他容易的方法。。

解决方案 »

  1.   

    1 Windows XP only:const
      IID_IThumbnailCapture : TGUID = (
        D1:$4ea39266; D2:$7211; D3:$409f; D4:($b6,$22,$f6,$3d,$bd,$16,$c5,$33));
    type
      SIZE = record
        cx, cy : LongInt;
      end;
      PSIZE = ^SIZE;  IThumbnailCapture = interface( IUnknown )
      ['{4ea39266-7211-409f-b622-f63dbd16c533}']    
        // *** IThumbnailCapture methods ***
        function CaptureThumbnail( pMaxSize : PSIZE;
                      pHTMLDoc2 : IUnknown;
                      var phbmThumbnail : HBiTMAP ) : HResult; stdcall;CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
        CLSCTX_LOCAL_SERVER, IThumbnailCapture, Capture);
    2 IE6 only
    IHTMLElementRenderer::Draw(HDC hDC);
    3 Two things to note while using this method, (1) you will probably want to turn off the scrollbars and 3D border since they will show up in the image and (2) you will want to resize the WebBrowser to the size of the contained HTML if you want to capture the entire content in the image. You may want to only make these changes temporarily and change them back after the image is captured: IHTMLDocument2* pDoc = ...;
    IHTMLElement* pBodyElem = 0;
    HRESULT hr = pDoc->get_body(&pBodyElem);
    if (SUCCEEDED(hr)) {
       IHTMLBodyElement* pBody = 0;
       hr = pBodyElem->QueryInterface(IID_IHTMLBodyElement, (void**)&pBody);
       if (SUCCEEDED(hr)) {
          // hide 3D border
          IHTMLStyle* pStyle;
          hr = pBodyElem->get_style(&pStyle);
          if (SUCCEEDED(hr)) {
             pStyle->put_borderStyle(CComBSTR("none"));
             pStyle->Release();
          }      // hide scrollbars
          pBodyElement->put_scroll(CComBSTR("no"));      // resize the browser component to the size of the HTML content
          IHTMLElement2* pBodyElement2;
          hr = Body->QueryInterface(IID_IHTMLElement2, (void**)&BodyElement2)
          if (SUCCEEDED(hr)) {
             long iScrollWidth = 0;
             pBodyElement2->get_scrollWidth(&iScrollWidth);         long iScrollHeight = 0;
             pBodyElement2->get_scrollHeight(&iScrollHeight);         // these lines depend on your WebBrowser wrapper
             pWebBrowser->SetWidth(iScrollWidth);
             pWebBrowser->SetHeight(iScrollHeight);         pBodyElement2->Release();         IViewObject* pViewObject;
             pDoc->QueryInterface(IID_IViewObject, (void**)&pViewObject);
             if (pViewObject) {
                /* however you want to make your image HDC.
                   You can size it using iScrollHeight & iScrollWidth */
                HDC hImageDC = ... // could be bitmap or enhanced metafile
                HDC hScreenDC = ::GetDC(0);
                RECT rcSource = {0, 0, iScrollWidth, iScrollHeight};
                hr = pViewObject->Draw(DVASPECT_CONTENT, 1, NULL, NULL,
                                       hScreenDC, hImageDC, rcSource,
                                       NULL, NULL, 0);
                ::ReleaseDC(0, hScreenDC);
                pViewObject->Release();
             }
          }
          pBody->Release();
       }
       pBodyElem->Release();
    }pDoc->Release();
      

  2.   

    Save a web page from TWebBrowser as a JPEG file
    Question:I need to make screenshots of web pages. Can I do that elegantly with TWebBrowser?Answer:Have TWebBrowser load the page, then have a view object paint the page onto a TCanvas (of a TBitmap) which you then subsequently resize to your desired thumbnail dimension and save in JPEG format.The example below does not reduce the picture, thus the JPEGs are about full screen size.You can download a project which was compiled with Delphi 5 here: HTTPtoJPEG.zip (3 kB).   
     procedure GenerateJPEGfromBrowser(browser: iWebBrowser2;
                                      jpegFQFilename: string; srcHeight:
                                      integer; srcWidth: integer;
                                      tarHeight: integer; tarWidth: integer);
    var
      sourceDrawRect: TRect;
      targetDrawRect: TRect;
      sourceBitmap  : TBitmap;
      targetBitmap  : TBitmap;
      aJPG          : TJPEGImage;
      aViewObject   : IViewObject;
    begin { GenerateJPEGfromBrowser }
      sourceBitmap := TBitmap.Create;
      targetBitmap := TBitmap.Create;
      aJPG := TJPEGImage.Create;
      try
        try
          sourceDrawRect := Rect(0, 0, srcWidth, srcHeight);
          sourceBitmap.Width := srcWidth;
          sourceBitmap.Height := srcHeight;      aViewObject := browser as IViewObject;      if aViewObject=nil then
            Exit;      OleCheck(aViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil,
                                   Form1.Handle,
                                   sourceBitmap.Canvas.Handle,
                                   @sourceDrawRect, nil, nil, 0));      // Resize the src bitmap to the target bitmap      // Need to make thumbnails instead of full size?      // set the target size here..       targetDrawRect := Rect(0, 0, tarWidth, tarHeight);
          targetBitmap.Height := tarHeight;
          targetBitmap.Width := tarWidth;
          targetBitmap.Canvas.StretchDraw(targetDrawRect, sourceBitmap);      // Create a JPEG from the Bitmap and save it      aJPG.Assign(targetBitmap);      aJPG.SaveToFile(jpegFQFilename)
        finally
          aJPG.Free;
          sourceBitmap.Free;
          targetBitmap.Free
        end; { try }  except
        // error handler code  end; { try }
    end; { GenerateJPEGfromBrowser }
    procedure TForm1.Button2Click(Sender: TObject);
    var
      IDoc1: IHTMLDocument2;
      Web  : iWebBrowser2;
      tmpX,
      tmpY : integer;
    begin { TForm1.Button2Click }
      with WebBrowser1 do 
      begin
        Document.QueryInterface(IHTMLDocument2, IDoc1); 
        Web := ControlInterface; 
        tmpX := Height; 
        tmpY := Width; 
        Height := OleObject.Document.ParentWindow.Screen.Height; 
        Width := OleObject.Document.ParentWindow.Screen.Width; 
        GenerateJPEGfromBrowser(Web, '.\test.jpg',
                                Height, Width,
                                Height, Width);
        Height := tmpX;
        Width := tmpY;    Image1.Picture.LoadFromFile('.\test.jpg')
      end; { with WebBrowser1 }
    end; { TForm1.Button2Click }  
      

  3.   

    参考:http://www.delphifaq.com/fq/q2231.shtml