2010. július 23., péntek

How to capture the image in a TWebBrowser


Problem/Question/Abstract:

How to capture the image in a TWebBrowser

Answer:

Here's how: It involves grabbing the Internet Explorer_Server window handle, then getting the device context of that window, then assigning the DC to a new TCanvas, and finally, calling the appropriate VCL methods.

procedure TForm1.Button1Click(Sender: TObject);
var
  ShellDocObjectView: HWND;
  InternetExplorerServer: HWND;
  WebCanvas: TCanvas;
begin
  ShellDocObjectView := FindWindowEx(WebBrowser1.Handle, 0, 'Shell DocObject View', nil);
  InternetExplorerServer := FindWindowEx(ShellDocObjectView, 0, 'Internet Explorer_Server', nil);
  WebCanvas := TCanvas.Create;
  WebCanvas.Handle := GetDC(InternetExplorerServer);
  InvalidateRect(InternetExplorerServer, nil, True);
  WebCanvas.Lock;
  Image1.Canvas.Lock;
  try
    Image1.Canvas.CopyRect(Rect(0, 0, Image1.Width, Image1.Height), WebCanvas,
      Rect(0, 0, WebBrowser1.Width, WebBrowser1.Height));
  finally
    Image1.Canvas.Unlock;
    WebCanvas.Unlock;
    ReleaseDC(InternetExplorerServer, WebCanvas.Handle);
    WebCanvas.Handle := 0;
    WebCanvas.Free;
  end;
end;

Likewise, you can call  Image1.Picture.Bitmap.SaveToFile('C:\My.bmp') to save the bitmap image
to a file.

Nincsenek megjegyzések:

Megjegyzés küldése