🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.web
5 messages
5 total messages Started by Lapo Guidi Tue, 24 Aug 2004 01:40
TCppWebBrowser and CaptureThumbnail
#1664
Author: Lapo Guidi
Date: Tue, 24 Aug 2004 01:40
8 lines
999 bytes
Hi all,
Somebody can help me on using the buil-in CaptureThumbnail defined in
shlobj.h to save a jpg thumbnail of a loaded html document inside a
TCppWebBrowser?

Thanks very much, I'm going crazy.

Lapo
Re: TCppWebBrowser and CaptureThumbnail
#1665
Author: Remy Lebeau (TeamB)
Date: Tue, 24 Aug 2004 07:28
43 lines
2223 bytes
"Lapo Guidi" <lapoguidi@inwind.it> wrote in message
news:412a9c86@newsgroups.borland.com...

> Somebody can help me on using the buil-in CaptureThumbnail
> defined in shlobj.h to save a jpg thumbnail of a loaded html
> document inside a TCppWebBrowser?

    #include <utilcls.h>
    #include <mshtml.h>
    #include <memory>
    #include <jpeg.hpp>

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        if(
SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc)) )
        {
            SIZE size = {100, 100};    // use whatever dimensions you
want...
            HBITMAP hThumb = NULL;
            TComInterface<IThumbnailCapture> Thumb;

            // sorry, I don't know how to obtain the actual
            // IThumbnailCapture instance, someone else
            // will have to help you with that part...

            if( SUCCEEDED(Thumb->CaptureThumbnail(&size, Doc, &hThumb)) )
            {
                std::auto_ptr<Graphics::TBitmap> Bmp(new Graphic::TBitmap);
                Bmp->Handle = hThumb;

                std::auto_ptr<TJPEGImage> Jpg(new TJPEGImage);
                Jpg->Assign(Bmp.get());
                // use Jpg as needed...
            }
        }
    }


Gambit
Re: TCppWebBrowser and CaptureThumbnail
#1668
Author: Lapo Guidi
Date: Tue, 24 Aug 2004 14:20
132 lines
4547 bytes
Thanks Remy!
I had the same problem about IThumbnailCapture instance.

Well, what i would like to  do is to save a page preview as jpeg of an HTML
page.
I asked about CreateThumbnail as last try.
I found these 2 Delphi codes snip but both seems not good for BCB++.
In the first I got an error because IHTMLDocument2 seems don't have a Body
property.
In the second I have JPEG, but all of them are 1600x1200 (like my screen
resolution).
Maybe you can help in that way.

------- first code ----------
var
 Doc: IHTMLDocument2;
 body: OleVariant;
begin
 Doc := IHTMLDocument2(yourWebBrowser.Document);
 body := Doc.Body;                             <<<------------- HERE THE
PROBLEM IHTMLDocument2 doesn't have Body property!
 body.NoWrap := True;

 {hide scrollbars}
 body.Style.BorderStyle := 'none';
 body.Scroll := 'no';

 intWidth := body.ScrollWidth;
 intHeight := body.ScrollHeight;
 yourWebBrowser.Width := body.ScrollWidth;
 yourWebBrowser.Height := body.ScrollHeight;

 sourceDrawRect := Rect(0, 0, body.ScrollWidth, body.ScrollHeight);
 sourceBitmap := TBitmap.Create;
 try
   sourceBitmap.Width := WebBrowser.Width;
   sourceBitmap.Height := WebBrowser.Height;

   WebBrowser.Document.QueryInterface(IViewObject, ViewObject);
   if ViewObject <> nil then
     try
       ViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Self.Handle,
sourceBitmap.Canvas.Handle, @sourceDrawRect, nil, nil, 0);
     finally
       ViewObject._Release;
     end;

   sourceBitmap.SaveToFile(yourFileName);
 finally
   sourceBitmap.Free;
 end;
end;


-------- second code ----------------

var
  IDoc1: IHTMLDocument2;
  Web: ShDocVW_TLB.IWebBrowser2;
  tmpX, tmpY: Integer;

begin
  with WebBrowser1 do
  begin
    Document.QueryInterface(IHTMLDocument2, iDoc1);
    Web := ControlInterface;
    tmpX := Height;
    tmpY := Width;
    TControl(WebBrowser1).Visible := Boolean(0);
    Height := OleObject.Document.ParentWindow.Screen.Height;
    Width := OleObject.Document.ParentWindow.Screen.Width;
    generateJPEGfromBrowser(Web,'c:\test.jpg',Height, Width, Height, Width);
    Height := tmpX;
    Width := tmpY;
    TControl(WebBrowser1).Visible := Boolean(1);
  end;
end;

procedure generateJPEGfromBrowser(browser: iWebBrowser2; jpegFQFilename:
string;
  srcHeight: Integer; srcWidth: Integer; tarHeight: Integer; tarWidth:
Integer);
var
  sourceDrawRect: TRect;
  targetDrawRect: TRect;
  sourceBitmap: TBitmap;
  targetBitmap: TBitmap;
  jpeg: TJPEGImage;
  viewObject: IViewObject;
begin
  sourceBitmap := TBitmap.Create;
  targetBitmap := TBitmap.Create;
  jpeg := TJPEGImage.Create;
  try
    try
      sourceDrawRect := Rect(0, 0, srcWidth, srcHeight);
      sourceBitmap.Width  := srcWidth;
      sourceBitmap.Height := srcHeight;

      viewObject := browser as IViewObject;

      if viewObject = nil then
        Exit;

      OleCheck(viewObject.Draw(DVASPECT_CONTENT, 1, nil, nil, Form1.Handle,
        sourceBitmap.Canvas.Handle, @sourceDrawRect, nil, nil, 0));

      // Resize the src bitmap to the target bitmap
      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
      jpeg.Assign(targetBitmap);

      jpeg.SaveToFile(jpegFQFilename);
    finally
      jpeg.Free;
      sourceBitmap.Free;
      targetBitmap.Free;
    end;
  except
    // Error Code
  end;
end;

------------------

Thanks for your time.
Sincerely,
Lapo
Re: TCppWebBrowser and CaptureThumbnail
#1672
Author: Remy Lebeau (TeamB)
Date: Tue, 24 Aug 2004 19:36
152 lines
6002 bytes
"Lapo Guidi" <lapoguidi@inwind.it> wrote in message
news:412b4ebf@newsgroups.borland.com...

> In the first I got an error because IHTMLDocument2
> seems don't have a Body property.

Use its get_body() method for that.

> ------- first code ----------

Try this code:

    #include <utilcls.h>
    #include <memory>

    TComInterface< IHTMLDocument2> Doc;
    if(
SUCCEEDED(CppWebBrowser->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc)) )
    {
        TComInterface<IHTMLElement> Body;
        if( SUCCEEDED(Doc->get_body(&Body)) )
        {
            TComInterface<IHTMLBodyElement> BodyElement;
            if( SUCCEEDED(Body->QueryInterface(IID_IHTMLBodyElement,
(LPVOID*)&BodyElement)) )
            {
                BodyElement->put_noWrap(VARIANT_TRUE);

                // hide scrollbars
                TComInterface<IHTMLStyle> Style;
                if( SUCCEEDED(Body->get_style(&Style)) )
                    Style->put_borderStyle(WideString("none"));
                BodyElement->put_scroll(WideString("no"));

                TComInterface<IHTMLElement2> BodyElement2;
                if( SUCCEEDED(Body->QueryInterface(IID_IHTMLElement2,
(LPVOID*)&BodyElement2)) )
                {
                    long ScrollWidth = 0;
                    BodyElement2->get_scrollWidth(&ScrollWidth);

                    long ScrollHeight = 0;
                    BodyElement2->get_scrollHeight(&ScrollHeight);

                    CppWebBrowser1->Width = ScrollWidth;
                    CppWebBrowser1->Height = ScrollHeight;

                    TComInterface<IViewObject> ViewObject;
                    CppWebBrowser->Document->QueryInterface(IID_IViewObject,
(LPVOID*)&ViewObject);
                    if( ViewObject )
                    {
                        std::auto_ptr<Graphics::TBitmap> Source(new
Graphics::TBitmap);
                        Source->Width = CppWebBrowser1->Width;
                        Source->Height = CppWebBrowser->Height;

                        RECT SourceRect = Rect(0, 0, ScrollWidth,
ScrollHeight);
                        if( SUCCEEDED(ViewObject->Draw(DVASPECT_CONTENT, 1,
NULL, NULL, this->Handle, Source->Canvas->Handle, &SourceRect, NULL, NULL,
0)) )
                            Source->SaveToFile("c:\\somefile.bmp");
                    }
                }
            }
        }
    }


> -------- second code ----------------

Try this code:

    #include <utilcls.h>
    #include <memory>

    TComInterface<IHTMLDocument2> Doc;
    if(
SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc)) )
    {
        int tmpX = CppWebBrowser1->Width;
        int tmpY = CppWebBrowser1->Height;
        ((TControl*)CppWebBrowser1)->Visible = false;

        TComInterface<IHTMLWindow2> ParentWindow;
        if( SUCCEEDED(Doc->get_ParentWindow(&ParentWindow)) )
        {
            TComInterface<IHTMLScreen> HTMLScreen;
            if( SUCCEEDED(ParentWindow->get_screen(&HTMLScreen)) )
            {
                long width = 0;
                HTMLScreen->get_width(&width);

                long height = 0;
                HTMLScreen->get_width(&height);

                CppWebBrowser1->Width = width;
                CppWebBrowser1->Height = height;

                GenerateJPEGfromBrowser(CppWebBrowser1->ControlInterface,
"c:\\test.jpg", width, height, width, height);
            }
        }

        CppWebBrowser1->Width = tmpX;
        CppWebBrowser1->Height = tmpY;
        ((TControl*)CppWebBrowser1)->Visible = true;
    }

    void __fastcall GenerateJPEGfromBrowser(IWebBrowser2* Browser, const
AnsiString &jpegFQFilename, int srcWidth, int srcHeight, int tarWidth, int
tarHeight)
    {
        try
        {
            TComInterface<IViewObject> ViewObject;
            Browser->QueryInterface(IID_IViewObject, (LPVOID*)&ViewObject);
            if( ViewObject )
            {
                std::auto_ptr<Graphics::TBitmap> Source(new
Graphics::TBitmap);
                Source->Width = srcWidth;
                Source->Height = srcHeight;

                RECT SourceRect = Rect(0, 0, srcWidth, srcHeight);
                OLECHECK(ViewObject->Draw(DVASPECT_CONTENT, 1, NULL, NULL,
Form1->Handle, Source->Canvas->Handle, &SourceRect, NULL, NULL, 0));

                // Resize the src bitmap to the target bitmap
                std::auto_ptr<Graphics::TBitmap> Target(new
Graphics::TBitmap);
                Target->Width = tarWidth;
                Target->Height = tarHeight;
                Target->Canvas->StretchDraw(Rect(0, 0, tarWidth, tarHeight),
Source);

                // Create a JPEG from the Bitmap and save it
                std::auto_ptr<TJPEGImage> Jpeg(new TJPEGImage);
                Jpeg->Assign(Target);
                Jpeg->SaveToFile(jpegFQFilename);
            }
        }
        catch(const Exception &) {}
        catch(...) {}
    }


Gambit
Re: TCppWebBrowser and CaptureThumbnail
#1677
Author: Lapo Guidi
Date: Thu, 26 Aug 2004 22:45
4 lines
1166 bytes
Remy, thank you very much!
As usual you solved the question.

Lapo
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads