Link to home
Start Free TrialLog in
Avatar of CristianIgnat
CristianIgnat

asked on

HTML Page Preview or (even better) thumbnail viewer of a folder with html files

Hi,

Please help me with this:

I need either to make a preview of a local html page. I`ve tried something with the TWebBrowser but the prob is that I can`t stretch the page there. That would have been perfect! Or even better I would like thumbnail viewer of a folder with html pages. I`m sure the first one is simpler. It would be best if it could happen without any components or with free components.

This is urgent. Thanks!

Cristian
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Cristian,

Have you already taken a look at, or tried the code from this PAQ?

https://www.experts-exchange.com/questions/21608891/Web-Page-Snapshots-good-solution-but-help-needed.html

It uses the browser control to create snapshots (also scaled image snapshots) from an html page. Perhaps this is close to what you are looking for?

Russell

Avatar of CristianIgnat
CristianIgnat

ASKER

Yep, thanks a lot.
Can you tell me also how can I make a webbrowser not visible ? cos I put false there but it still appears on the form. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi rllibby,

you`re solution looks simpler! the prob is it doesn`t find the CreateScaledImage. in what unit is that ?

Thanks
Its the second version of WebUtils from the link; I am posting it here so there is no confusion:

unit WebUtils;
////////////////////////////////////////////////////////////////////////////////
//
//   Unit        :  WebUtils
//   Date        :  10.27.2005 (updated 10.28.2005)
//   Description :  Utility functions based on the TWebBrowser object
//
//
//   function GetDocumentSize(Browser: TWebBrowser): TPoint;
//
//      Returns the document's body size as a TPoint, where X = width and Y = height
//
//   function GetBrowserElement(Browser: TWebBrowser; out Element: IHTMLElement2): HResult;
//
//      Returns the actual element required to perform the rendering to a canvas handle.
//      For most pages, this will be the body element. But for some, eg:
//
//         https://www.experts-exchange.com
//         http://www.msn.com
//
//      the parent element will need to be used. The element to use is based on
//      getting the parent and checking it for a clientwidth/height of zero. If zero,
//      then the body element can be used. If not zero, then the parent must be used.
//
//   function CreateThumbnailImage(Browser: TWebBrowser; OutputSize: TPoint): TBitmap;
//
//      Returns a bitmap object of the document page where the width and height is
//      determined by the point (x = width, y=height) passed in OutputSize.
//
//   function CreateScaledImage(Browser: TWebBrowser; Scale: Double): TBitmap;
//
//      Returns a scaled bitmap object of the document page. A scaling of 1.0 will
//      return the page at its actual size. To return a 1/10 scaled image of the
//      document for example, 0.1 would be passed to Scale.
//
////////////////////////////////////////////////////////////////////////////////
interface

////////////////////////////////////////////////////////////////////////////////
//   Include units
////////////////////////////////////////////////////////////////////////////////
uses
  Windows, SysUtils, Classes, Graphics, ShdocVW, ActiveX, MSHTML;

////////////////////////////////////////////////////////////////////////////////
//   Constants
////////////////////////////////////////////////////////////////////////////////
const
  IID_IHTMLElementRender: TGUID =  '{3050F669-98B5-11CF-BB82-00AA00BDCE0B}';

////////////////////////////////////////////////////////////////////////////////
//   Interfaces
////////////////////////////////////////////////////////////////////////////////
type
  IHTMLElementRender   =  interface(IUnknown)
     ['{3050F669-98B5-11CF-BB82-00AA00BDCE0B}']
     function          DrawToDC(_hDC: HDC): HResult; stdcall;
     function          SetDocumentPrinter(bstrPrinterName: WideString; _hDC: HDC): HResult; stdcall;
  end;

////////////////////////////////////////////////////////////////////////////////
//   Utility functions
////////////////////////////////////////////////////////////////////////////////
function   GetDocumentSize(Browser: TWebBrowser): TPoint;
function   GetBrowserElement(Browser: TWebBrowser; out Element: IHTMLElement2): HResult;
function   CreateThumbnailImage(Browser: TWebBrowser; OutputSize: TPoint): TBitmap;
function   CreateScaledImage(Browser: TWebBrowser; Scale: Double): TBitmap;

implementation

function GetBrowserElement(Browser: TWebBrowser; out Element: IHTMLElement2): HResult;
var  pDoc:          IHTMLDocument2;
     pElemParent:   IHTMLElement;
     pElement:      IHTMLElement;
     pElement2:     IHTMLElement2;
begin

  // Check browser document
  if Assigned(Browser.Document) then
  begin
     // QI for document 2 interface
     result:=Browser.Document.QueryInterface(IHTMLDocument2, pDoc);
     // Check result
     if Succeeded(result) then
     begin
        // Resource protection
        try
           // Check document body
           if Assigned(pDoc.Body) then
           begin
              // QI for body element
              result:=pDoc.Body.QueryInterface(IHTMLElement, pElement);
              // Check result
              if Succeeded(result) then
              begin
                 // Resource protection
                 try
                    // Get parent element
                    pElemParent:=pElement.parentElement;
                    // Check parent
                    if Assigned(pElemParent) then
                    begin
                       // Resource protection
                       try
                          // Get the element 2 interface
                          if Succeeded(pElemParent.QueryInterface(IHTMLElement2, pElement2)) then
                          begin
                             // Check parent interface for zero client width or height
                             if (pElement2.clientWidth = 0) or (pElement2.clientHeight = 0) then
                                // QI for element 2 interface
                                result:=pElement.QueryInterface(IHTMLElement2, Element)
                             else
                                // We have the element we are after
                                Element:=pElement2;
                          end
                          else
                             // QI for element 2 interface
                             result:=pElement.QueryInterface(IHTMLElement2, Element);
                       finally
                          // Release the interface
                          pElemParent:=nil;
                       end;
                    end
                    else
                       // Return nearest COM error
                       result:=E_NOINTERFACE;
                 finally
                    // Release the interface
                    pElement:=nil;
                 end;
              end;
           end
           else
              // Return nearest COM error
              result:=E_NOINTERFACE;
        finally
           // Release the interface
           pDoc:=nil;
        end;
     end;
  end
  else
     // Return nearest COM error
     result:=E_NOINTERFACE;

end;

function GetDocumentSize(Browser: TWebBrowser): TPoint;
var  pElement:      IHTMLElement2;
begin

  // Get the browser body element interface
  if Succeeded(GetBrowserElement(Browser, pElement)) then
  begin
     // Resource protection
     try
        // Set result size
        result:=Point(pElement.ScrollWidth, pElement.ScrollHeight);
     finally
        // Release the interface
        pElement:=nil;
     end;
  end

end;

function CreateScaledImage(Browser: TWebBrowser; Scale: Double): TBitmap;
var  ptActual:      TPoint;
     ptScale:       TPoint;
begin

  // Get actual size
  ptActual:=GetDocumentSize(Browser);

  // Apply the scaling to it
  ptScale.x:=Round(ptActual.x * Scale);
  ptScale.y:=Round(ptActual.y * Scale);

  // Get the image
  result:=CreateThumbnailImage(Browser, ptScale);

end;

function CreateThumbnailImage(Browser: TWebBrowser; OutputSize: TPoint): TBitmap;
var  pElement:      IHTMLElement2;
     pRender:       IHTMLElementRender;
     bmpRender:     Array [0..1] of TBitmap;
     dwClientWidth: Integer;
     dwClientHeight:Integer;
     dwIndex:       Integer;
     dwX:           Integer;
     dwLastX:       Integer;
     bDoneX:        Boolean;
     dwY:           Integer;
     dwLastY:       Integer;
     bDoneY:        Boolean;
begin

  // Create resulting image
  result:=TBitmap.Create;

  // Set size
  result.Width:=OutputSize.x;
  result.Height:=OutputSize.y;

  // Lock update
  LockWindowUpdate(Browser.Handle);

  // Resource protection
  try
     // Get the browser body element interface
     if Succeeded(GetBrowserElement(Browser, pElement)) then
     begin
        // Resource protection
        try
           // Get the renderer
           if Succeeded(pElement.QueryInterface(IID_IHTMLElementRender, pRender)) then
           begin
              // Resource protection
              try
                 // Create images to blit the parts to
                 for dwIndex:=0 to 1 do
                 begin
                    bmpRender[dwIndex]:=TBitmap.Create;
                    bmpRender[dwIndex].Width:=pElement.scrollWidth;
                    bmpRender[dwIndex].Height:=pElement.scrollHeight;
                 end;
                 // Set render "printer"
                 pRender.SetDocumentPrinter('Bitmap', bmpRender[0].Canvas.Handle);
                 // Get client width and height
                 dwClientWidth:=pElement.clientWidth;
                 dwClientHeight:=pElement.clientHeight;
                 // Resource protection
                 try
                    // Set starting X variables
                    dwX:=pElement.scrollWidth;
                    dwLastX:=(-1);
                    bDoneX:=False;
                    // Loop while X not done
                    while not(bDoneX) do
                    begin
                       // Scroll
                       pElement.scrollLeft:=dwX;
                       // Get scroll
                       dwX:=pElement.scrollLeft;
                       // Check for (-1)
                       if (dwLastX = (-1)) then dwLastX:=dwX + dwClientWidth;
                       // Set starting Y variables
                       dwY:=pElement.scrollHeight;
                       dwLastY:=(-1);
                       bDoneY:=False;
                       // Loop while Y not done
                       while not(bDoneY) do
                       begin
                          // Scroll
                          pElement.scrollTop:=dwY;
                          // Get scroll
                          dwY:=pElement.scrollTop;
                          // Check for (-1)
                          if (dwLastY = (-1)) then dwLastY:=dwY + dwClientHeight;
                          // Draw to bitmap handle
                          if (pRender.DrawToDC(bmpRender[0].Canvas.Handle) = S_OK) then
                          begin
                             // Blit the image
                             BitBlt(bmpRender[1].Canvas.Handle, dwX, dwY, dwLastX-dwX, dwLastY-dwY, bmpRender[0].Canvas.Handle, 2, 2,SRCCOPY);
                          end;
                          // Update the Y variables
                          bDoneY:=(dwY = 0);
                          dwLastY:=dwY;
                          Dec(dwY, (dwClientHeight-4));
                       end;
                       // Update the X variables
                       bDoneX:=(dwX = 0);
                       dwLastX:=dwX;
                       Dec(dwX, (dwClientWidth-4));
                    end;
                    // Stretch draw the image to the resulting bitmap
                    StretchBlt(result.Canvas.Handle, 0, 0, OutputSize.x, OutputSize.y, bmpRender[1].Canvas.Handle, 0, 0, bmpRender[1].Width, bmpRender[1].Height, SRCCOPY);
                 finally
                    // Free the bitmap
                    for dwIndex:=0 to 1 do FreeAndNil(bmpRender[dwIndex]);
                 end;
              finally
                 // Release the interface
                 pRender:=nil;
              end;
           end;
        finally
           // Release the interfaces
           pElement:=nil;
        end;
     end;
  finally
     // Unlock update
     LockWindowUpdate(0);
  end;

end;

end.
Thanks !!!

It works amazing!

Glad to here. ;-)

Russell

Cristian,
Were you going to close this question, or did you need further help?

Russell
oh, sorry !
I thought I closed it!

THANKS !!!
No problem, and thank you...

Russell