从网上看到别人写的网页截图的方法 ,原作者HDFLOVE
procedure GenerateJPEGfromBrowser(browser: iWebBrowser2;
jpegFQFilename: string; srcHeight:
integer; srcWidth: integer;
tarHeight: integer; tarWidth: integer);
var
sourceDrawRect: TRect;
targetDrawRect: TRect;
sourceBitmap : TBitmap;
targetBitmap : TBitmap;
aJPG : TJPEGImage;
aViewObject : IViewObject;
begin
sourceBitmap :
=
TBitmap.Create;
targetBitmap :
=
TBitmap.Create;
aJPG :
=
TJPEGImage.Create;
try
try
sourceDrawRect :
=
Rect(0, 0, srcWidth, srcHeight);
sourceBitmap.Width :
=
srcWidth;
sourceBitmap.Height :
=
srcHeight;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
aViewObject :
=
browser as IViewObject;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
if
aViewObject
=
nil then
Exit;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
OleCheck(aViewObject.Draw(DVASPECT_CONTENT,
1
, nil, nil,
Form1.Handle,
sourceBitmap.Canvas.Handle,
@sourceDrawRect, nil, nil, 0));
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
设定图片尺寸
targetDrawRect :
=
Rect(0, 0, tarWidth, tarHeight);
targetBitmap.Height :
=
tarHeight;
targetBitmap.Width :
=
tarWidth;
targetBitmap.Canvas.StretchDraw(targetDrawRect, sourceBitmap);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
产生JPEG并保存
aJPG.Assign(targetBitmap);
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
aJPG.SaveToFile(jpegFQFilename)
finally
aJPG.Free;
sourceBitmap.Free;
targetBitmap.Free
end; {
try
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
except
//
处理异常
end;
end;
procedure CaptureWebPage;
var
IDoc1: IHTMLDocument2;
Web : iWebBrowser2;
tmpX,
tmpY : integer;
begin
with WebBrowser1 do
begin
Document.QueryInterface(IHTMLDocument2, IDoc1);
Web :
=
ControlInterface;
tmpX :
=
Height;
tmpY :
=
Width;
Height :
=
OleObject.Document.Body.getAttribute(
'
scrollHeight
'
, 0)
+
GetSystemMetrics(SM_CXVSCROLL)
+
3
;
//
.ParentWindow.Screen.Height
+
1000
;
Width :
=
OleObject.Document.Body.getAttribute(
'
scrollwidth
'
, 0)
+
GetSystemMetrics(SM_CXVSCROLL)
+
3
;
//
.ParentWindow.Screen.Width;
GenerateJPEGfromBrowser(Web,
'
demo1.jpg
'
, Height, Width, Height, Width);
Height :
=
tmpX;
Width :
=
tmpY;
end;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
end;