[英]How to create a Bitmap from the Client area of a Web Browser created at run-time?
In a Delphi 10.4.2 Win32 VCL Application on Windows 10 x64, I need to create a Bitmap of a SPECIFIC SIZE from a web browser's client area. In a Delphi 10.4.2 Win32 VCL Application on Windows 10 x64, I need to create a Bitmap of a SPECIFIC SIZE from a web browser's client area. The web browser loads a local SVG.
web 浏览器加载本地 SVG。 You can get the SVG here: https://svgshare.com/s/Uzf
您可以在此处获取 SVG: https://svgshare.com/s/Uzf
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw,
Vcl.StdCtrls, Vcl.ComCtrls;
type
TForm1 = class(TForm)
btnDoIt: TButton;
procedure btnDoItClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
CodeSiteLogging;
procedure PaintWebBrowserClientAreaToBitmap(AWB: TWebBrowser; AOut: Vcl.Graphics.TBitmap);
var
DC: Winapi.Windows.HDC;
begin
if not Assigned(AOut) then
Exit;
if not Assigned(AWB) then
Exit;
DC := GetWindowDC(AWB.Handle);
AOut.Width := AWB.Width;
AOut.Height := AWB.Height;
with AOut do
Winapi.Windows.BitBlt(Canvas.Handle, 0, 0, Width, Height, DC, 0, 0, Winapi.Windows.SrcCopy);
ReleaseDC(AWB.Handle, DC);
end;
procedure TForm1.btnDoItClick(Sender: TObject);
var
B: TBitmap;
begin
B := TBitmap.Create;
try
var wb2: SHDocVw.TWebBrowser;
wb2 := SHDocVw.TWebBrowser.Create(nil);
try
//wb2.Name := 'wb2';
wb2.SelectedEngine := IEOnly;
wb2.ClientWidth := 300;
wb2.ClientHeight := 525;
wb2.Navigate('file:///C:\DELPHI\_test\BrowserSVGViewer\steamreactor.svg');
PaintWebBrowserClientAreaToBitmap(wb2, B);
CodeSite.Send('B', B);
ShowMessage('test'); // halt here to see the nice image
finally
wb2.Free;
end;
finally
B.Free;
end;
end;
end.
This creates an image for a very short time visibly on the screen.这会在很短的时间内在屏幕上创建一个可见的图像。 But the Bitmap remains empty!
但是 Bitmap 仍然是空的!
How can I make this work?我怎样才能使这项工作? (Possibly without any appearance of the image on the screen).
(可能没有任何图像出现在屏幕上)。
You have to wait until the WebBrowser has finished to work:您必须等到 WebBrowser 完成工作:
procedure TForm1.btnDoItClick(Sender: TObject);
var
B : TBitmap;
wb2 : SHDocVw.TWebBrowser;
begin
B := TBitmap.Create;
try
wb2 := SHDocVw.TWebBrowser.Create(nil);
try
wb2.SelectedEngine := IEOnly;
wb2.ClientWidth := 300;
wb2.ClientHeight := 525;
wb2.Navigate('file:///E:\TEMP\steamreactor.svg');
repeat
Application.ProcessMessages;
until not wb2.Busy;
PaintWebBrowserClientAreaToBitmapOld(wb2, B);
Image1.Picture.Bitmap := B;
finally
wb2.Free;
end;
finally
B.Free;
end;
end;
Calling Application.ProcessMessages
is not the optimal way of waiting for the browser to terminate.调用
Application.ProcessMessages
并不是等待浏览器终止的最佳方式。 But this is another story:-)但这是另一个故事:-)
To easily view the image, I added a TImage
on the form.为了方便查看图像,我在表单上添加了一个
TImage
。 Of course you do whatever you like with the bitmap.当然,您可以随心所欲地使用 bitmap。
If what you need is to make a bitmap out of a SVG, there are Delphi libraries to do that.如果您需要从 SVG 中制作 bitmap,则可以使用 Delphi 库来执行此操作。 Google is your friend:-)
谷歌是你的朋友:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.