就是这个问题,我查了下,好象WebBrowser打印是可以的,我想有方法打印的到图片文件不是可以解决了,无奈没有参考好的参考代码。不知还有没有其他容易的方法。。
解决方案 »
- 一开始用Delphi做C/S架构的数据库程序就遇到问题了,请教
- delphi 查找excel
- COM+开发中有关接口类型的属性的问题
- >>>>>如何让计算机发出特定的报警声<<<<<<<
- 如何让PANEL控件随着所容纳的控件的增多添加滚动条
- 谁可以救我,SQL语句.急啊!
- 请问谁有《DELPHI 5企业级解决方案及应用剖析》随书附带的光盘,或是谁知道哪里有下载的?多谢!
- 急!高分求教format及format相关函数的具体用法?请大家踊跃发言
- 急,AdoQuery 如何使用服务端cursor及其CursorLocation 如何使用?
- 关于数据模块,请各位高手帮忙!
- 有没有WinSock API 做开发的好书,请推荐一本!
- 如何用adotable与后台数据库中数据比较?
IID_IThumbnailCapture : TGUID = (
D1:$4ea39266; D2:$7211; D3:$409f; D4:($b6,$22,$f6,$3d,$bd,$16,$c5,$33));
type
SIZE = record
cx, cy : LongInt;
end;
PSIZE = ^SIZE; IThumbnailCapture = interface( IUnknown )
['{4ea39266-7211-409f-b622-f63dbd16c533}']
// *** IThumbnailCapture methods ***
function CaptureThumbnail( pMaxSize : PSIZE;
pHTMLDoc2 : IUnknown;
var phbmThumbnail : HBiTMAP ) : HResult; stdcall;CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IThumbnailCapture, Capture);
2 IE6 only
IHTMLElementRenderer::Draw(HDC hDC);
3 Two things to note while using this method, (1) you will probably want to turn off the scrollbars and 3D border since they will show up in the image and (2) you will want to resize the WebBrowser to the size of the contained HTML if you want to capture the entire content in the image. You may want to only make these changes temporarily and change them back after the image is captured: IHTMLDocument2* pDoc = ...;
IHTMLElement* pBodyElem = 0;
HRESULT hr = pDoc->get_body(&pBodyElem);
if (SUCCEEDED(hr)) {
IHTMLBodyElement* pBody = 0;
hr = pBodyElem->QueryInterface(IID_IHTMLBodyElement, (void**)&pBody);
if (SUCCEEDED(hr)) {
// hide 3D border
IHTMLStyle* pStyle;
hr = pBodyElem->get_style(&pStyle);
if (SUCCEEDED(hr)) {
pStyle->put_borderStyle(CComBSTR("none"));
pStyle->Release();
} // hide scrollbars
pBodyElement->put_scroll(CComBSTR("no")); // resize the browser component to the size of the HTML content
IHTMLElement2* pBodyElement2;
hr = Body->QueryInterface(IID_IHTMLElement2, (void**)&BodyElement2)
if (SUCCEEDED(hr)) {
long iScrollWidth = 0;
pBodyElement2->get_scrollWidth(&iScrollWidth); long iScrollHeight = 0;
pBodyElement2->get_scrollHeight(&iScrollHeight); // these lines depend on your WebBrowser wrapper
pWebBrowser->SetWidth(iScrollWidth);
pWebBrowser->SetHeight(iScrollHeight); pBodyElement2->Release(); IViewObject* pViewObject;
pDoc->QueryInterface(IID_IViewObject, (void**)&pViewObject);
if (pViewObject) {
/* however you want to make your image HDC.
You can size it using iScrollHeight & iScrollWidth */
HDC hImageDC = ... // could be bitmap or enhanced metafile
HDC hScreenDC = ::GetDC(0);
RECT rcSource = {0, 0, iScrollWidth, iScrollHeight};
hr = pViewObject->Draw(DVASPECT_CONTENT, 1, NULL, NULL,
hScreenDC, hImageDC, rcSource,
NULL, NULL, 0);
::ReleaseDC(0, hScreenDC);
pViewObject->Release();
}
}
pBody->Release();
}
pBodyElem->Release();
}pDoc->Release();
Question:I need to make screenshots of web pages. Can I do that elegantly with TWebBrowser?Answer:Have TWebBrowser load the page, then have a view object paint the page onto a TCanvas (of a TBitmap) which you then subsequently resize to your desired thumbnail dimension and save in JPEG format.The example below does not reduce the picture, thus the JPEGs are about full screen size.You can download a project which was compiled with Delphi 5 here: HTTPtoJPEG.zip (3 kB).
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 { GenerateJPEGfromBrowser }
sourceBitmap := TBitmap.Create;
targetBitmap := TBitmap.Create;
aJPG := TJPEGImage.Create;
try
try
sourceDrawRect := Rect(0, 0, srcWidth, srcHeight);
sourceBitmap.Width := srcWidth;
sourceBitmap.Height := srcHeight; aViewObject := browser as IViewObject; if aViewObject=nil then
Exit; OleCheck(aViewObject.Draw(DVASPECT_CONTENT, 1, nil, nil,
Form1.Handle,
sourceBitmap.Canvas.Handle,
@sourceDrawRect, nil, nil, 0)); // Resize the src bitmap to the target bitmap // Need to make thumbnails instead of full size? // set the target size here.. targetDrawRect := Rect(0, 0, tarWidth, tarHeight);
targetBitmap.Height := tarHeight;
targetBitmap.Width := tarWidth;
targetBitmap.Canvas.StretchDraw(targetDrawRect, sourceBitmap); // Create a JPEG from the Bitmap and save it aJPG.Assign(targetBitmap); aJPG.SaveToFile(jpegFQFilename)
finally
aJPG.Free;
sourceBitmap.Free;
targetBitmap.Free
end; { try } except
// error handler code end; { try }
end; { GenerateJPEGfromBrowser }
procedure TForm1.Button2Click(Sender: TObject);
var
IDoc1: IHTMLDocument2;
Web : iWebBrowser2;
tmpX,
tmpY : integer;
begin { TForm1.Button2Click }
with WebBrowser1 do
begin
Document.QueryInterface(IHTMLDocument2, IDoc1);
Web := ControlInterface;
tmpX := Height;
tmpY := Width;
Height := OleObject.Document.ParentWindow.Screen.Height;
Width := OleObject.Document.ParentWindow.Screen.Width;
GenerateJPEGfromBrowser(Web, '.\test.jpg',
Height, Width,
Height, Width);
Height := tmpX;
Width := tmpY; Image1.Picture.LoadFromFile('.\test.jpg')
end; { with WebBrowser1 }
end; { TForm1.Button2Click }