How will I show image in THTMLViewer?
HFText is a string while HTMLViewer1 is the THTMLviewer.
HFText:= '<html><head><style>'+
'body {font: Arial 8pt;}'+
'</style></head>'+
'<body marginwidth="0">'+
'<img src="http://10.0.0.20/picture/1.jpg" />'+
'</body></html>';
HTMLViewer1.LoadFromString(HFText);
The output of this shows an exclamatio开发者_StackOverflow中文版n point (not the picture I want to display). I've been told to use the OnImageRequest but I cannot find examples to show me what to do. How can I use the OnImageRequest to show the picture?
HTMLViewer has a couple of different ways of handling images.
The simplest is to set an OnImageRequest handler and using something like the following code:
procedure TForm1.HTMLViewer1ImageRequest(Sender: TObject;
const SRC: string; var Stream: TMemoryStream);
var
wStrm : TMemoryStream;
begin
wStrm := TMemoryStream.create;
try
IdHTTP1.Get(Src, wStrm);
Stream := wStrm;
except
wStrm.free;
Stream := Nil;
end;
end;
The above code assumes you already have a IdHTTP1 component on the form for getting stuff or you could also load from a TFileStream.
The value of SRC will be, in your example, "http://10.0.0.20/picture/1.jpg".
You don't free the created stream, HTMLViewer will handle that for you. I only free the stream if there was a problem with the HTTP get.
HTH.
No answer here, but here's how I'd troubleshoot to see if you're making a basic mistake.
First, verify that when you enter this URL into your browser, you get the image. http://10.0.0.20/picture/1.jpg So far so good?
Next, make a file test.htm with the contents of your string. Double-click to launch in your browser. Still good?
Next... does it work if you use a file reference instead of http:?
精彩评论