Webbrowser的屏幕截图 [英] Screenshot of Webbrowser

查看:115
本文介绍了Webbrowser的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个TwebBrowser,我需要对其进行截图.有人可能会认为这不是复制品.但这并非如此,因为解决方案中的解决方案不起作用,所有这些都使med出现了黑屏.

I have a TwebBrowser on a form, of which I need to take a Screenshot. Some may think Isn't this a dublicate. But it's not, because the solutions in the orter answers doesn't work, thay all just gives med a black screen.

我要尝试从DC(0)获取像素

Så I try to get my pixels from DC(0)

首先提供一些源代码:

在窗体上放置一个TWebBrowser和一个TButton,并将以下代码添加到OnCreate事件中:

Place a TWebBrowser and a TButton on a form, and add the following code to a OnCreate Event:

procedure TForm1.FormCreate(Sender: TObject);
var
  Doc: Variant;
begin
  Width := 1350;
  Height := 860;
  with WebBrowser1 do
  begin
    Left := 0;
    Top := 0;
    Width := 1330;
    Height := 760;
    Anchors := [akLeft, akTop, akRight, akBottom];
  end;

  with Button1 do
  begin
    Left := 1048;
    Top := 776;
    Width := 58;
    Height := 25;
    Anchors := [akRight, akBottom];
    Caption := 'GetDC';
    OnClick := Button1Click;
  end;


  WebBrowser1.Navigate('about:blank');
  Doc := WebBrowser1.Document;
  Doc.Clear;
  Doc.Write('<embed src="https://r1---sn-cgxqc55oqovgq-55ae.googlevideo.com/videoplayback?sver=3&requiressl=yes&itag=22&ratebypass=yes&pl=19' +
    '&upn=PRgjNIjXqZo&ipbits=0&mm=31&id=o-AFwGYl-Gni-Xv-OpmDFDHPmsirrQ-tP9XPjRwG8B7XFk&initcwndbps=2765000&signature=772D32F7C20412D37B3F23A36D262D58A34BBEF8.9A9463362C4438781A05F634DDD97A590D6EF387'
    + '&ip=77.68.203.5&mv=m&mt=1428297827&ms=au&dur=274.692&key=yt5&mime=video%2Fmp4&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl'
    + '%2Csource%2Cupn%2Cexpire&expire=1428319485&fexp=900234%2C900720%2C907263%2C917000%2C932627%2C934954%2C9406733%2C9407060%2C9408101%2C946800%2C947243%2C948124%2C948703%2C951703%2C952612%2C957201%2C961404%2C961406%2C966201"  width="1280" height="720">');
  Doc.Close;

end;

这使您可以通过WebControl播放视频.这样做是有原因的,但是那超出了这个问题的范围.

This gives you a WebControl playing a video. There is a reason for doing it like this, but thats out of scope for this question.

然后是截图"内容:

procedure TForm1.Button1Click(Sender: TObject);
var
  bitmap: TBitmap;
  BrowserRect: TRect;
  DC: HDC;
  w, h: Integer;
  pt: TPoint;
begin
  bitmap := TBitmap.Create;
  BrowserRect := WebBrowser1.ClientRect;
  DC := GetDC(0);
  bitmap.Height := WebBrowser1.Height;
  bitmap.Width := WebBrowser1.Width;
  BitBlt(bitmap.Canvas.Handle, BrowserRect.Left, BrowserRect.Top, WebBrowser1.Width, WebBrowser1.Height, DC, 0, 0, SrcCopy);
  bitmap.SaveToFile('aa.bmp');
  FreeAndNil(bitmap);
end;

我尝试了很多东西.但是我无法正确计算Webbrowser的边界.我刚刚发布了这段代码.

I've tried a lot of stuff. But I cant get the calculation og the Webbrowser's bounds correct. Så I just posted this code.

Windows : Windows 8.1 64 bit
Delphi : Delphi Xe6
Exe : 64 bit

简而言之:是否有另一种捕获TWebBrowser的屏幕截图的方法或者如何计算TWebBrowser的绝对边界

So in short: Is there an other way of captureing a Screenshot of a TWebBrowser or How to calculate the abosolute boundaries of TWebBrowser

*更新*

基于我从Dalija Prasnikar获得的代码,我编写了一个捕获WinControl的过程

Based on the code I got from Dalija Prasnikar I wrote a procedure for captureing a WinControl

procedure PrintControl(AControl: TWinControl; var AOut: TBitmap);
var
  DC: HDC;
  pt: TPoint;
begin
  if not Assigned(AControl) then
    Exit;

  if not Assigned(AOut) then
    Exit;

  DC := GetDC(0);
  pt := AControl.ClientToScreen(AControl.BoundsRect.TopLeft);
  try
    AOut.Height := AControl.Height;
    AOut.Width := AControl.Width;
    BitBlt(AOut.Canvas.Handle, 0, 0, AControl.Width, AControl.Height, DC, pt.X, pt.Y, SRCCOPY);
  finally
    ReleaseDC(0, DC);
  end;
end;

推荐答案

您必须使用 WebBrowser.BoundsRect ,然后将其 TopLeft 点转换为屏幕坐标才能正确使用您的 WebBrowser 控件的来源.

You have to use WebBrowser.BoundsRect and then convert its TopLeft point to screen coordinates to get correct origin of your WebBrowser control.

您未正确使用 BitBlt 参数.声明是

You have used BitBlt parameters incorrectly. Declaration is

function BitBlt(DestDC: HDC; X, Y, Width, Height: Integer; SrcDC: HDC;
  XSrc, YSrc: Integer; Rop: DWORD): BOOL; stdcall;

X Y 是目标 HDC 中的坐标,而不是源中的坐标.

X and Y are coordinates in destination HDC, not the source.

完成后,您还需要释放捕获的 DC ,否则您将泄漏Windows资源.

You also need to release captured DC after you are done with it, or you will be leaking Windows resources.

var
  Bitmap: TBitmap;
  BrowserRect: TRect;
  DC: HDC;
  W, h: integer;
  pt: TPoint;
begin
  Bitmap := TBitmap.Create;
  try
    BrowserRect := WebBrowser1.BoundsRect;
    pt := ClientToScreen(BrowserRect.TopLeft);
    DC := GetDC(0);
    try
      Bitmap.Height := WebBrowser1.Height;
      Bitmap.Width := WebBrowser1.Width;
      BitBlt(Bitmap.Canvas.Handle, 0, 0, WebBrowser1.Width, WebBrowser1.Height, DC, pt.X, pt.Y, SRCCOPY);
      Bitmap.SaveToFile('c:\work\aa.bmp');
    finally
      ReleaseDC(0, DC);
    end;
  finally
    FreeAndNil(Bitmap);
  end;
end;

这篇关于Webbrowser的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆