I am using the TEdgeBrowser component in a browser/viewer Delphi application. I had few issues after a brief learning curve for the original implementation.
I am now at a point where I can experiment with some of the features (like screen capture).
To capture a screen to a file, you can simply add:
EdgeBrowser1.CapturePreview(ScrFileName);
Where ScrFileName is the path of the file i.e. c:\pic\screenshot.png
I would like to capture the screen every second (and relay it using an Indy component (TIdHTTPServer) to relay it internally (http).
I am able to do this as follows, but it's very inefficient. Can anyone recommend an alternative?
procedure TMainForm.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
Const
EOL = #13;
var
Strm: TMemoryStream;
begin
if ARequestInfo.Document = '' then
begin
AResponseInfo.Redirect('/');
end
else if ARequestInfo.Document = '/' then
begin
AResponseInfo.ResponseNo := 200;
AResponseInfo.ContentType := 'text/html';
AResponseInfo.ContentText := '<!DOCTYPE html>'+EOL+
'<html>'+EOL+
'<head>'+EOL+
'<meta name="viewport" content="width=device-width, initial-scale=5">'+EOL+
'<meta http-equiv="Refresh" content=1>'+EOL+
'<style type="text/css">'+EOL+
' img.fullscr {'+EOL+
' display:block;'+EOL+
' width:100%;'+EOL+
' }'+EOL+
'</style>'+EOL+
'</head>'+EOL+
'<body>'+EOL+
'<img src="/image" class="fullscr">'+EOL+
'</body>'+EOL+
'</html>'+EOL;
end
else if ARequestInfo.Document = '/image' then
begin
Strm := TMemoryStream.Create;
try
Strm.Clear;
Strm.LoadFromFile(ScrFileName);
Strm.Position := 0;
AResponseInfo.ResponseNo := 200;
AResponseInfo.ContentType := 'image/png';
AResponseInfo.ContentStream := Strm;
except
Strm.Free;
raise;
end;
end
else begin
AResponseInfo.ResponseNo := 404;
end;
end;
Have you considered simply not saving the captured image to disk at all?
TEdgeBrowser.CapturePreview()has an overload that saves to aTStream. You can save the captures at regular intervals to aTMemoryStreamthat you share withTIdHTTPServerto send the latest capture to HTTP clients.TIdHTTPServeris a multi-threaded component, itsOnCommand...events are fired in the context of worker threads, so be sure to protect theTMemoryStreamwith a thread-safe lock, such asTCriticalSection.For example:
To further optimize this, you might consider changing the HTML to use a client-side AJAX or WebSocket script to request and display the latest image at regular intervals without having to reload the entire HTML page each time.
For instance, if you were to use a WebSocket, you could add a WebSocket server to your code (Indy doesn't have one at this time, but there are 3rd party implementations floating around) and have the WebSocket server push out a new capture to the browser whenever it is ready, without having to wait for an HTTP request to arrive first.