- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

Returning an image from a Web Server Application

Question:

How can I embed a graphic into an HTML document produced by my Web Server application?

Answer:

Here is an example of how to embed a graphic into your web 

contents.  This example actually demonstrates how to return 

a jpeg using code.  Of course you could also simply put a 

reference to an actual jpeg in the <img src...> tag.  In 

this example the browser will "hit" the <IMG SRC> tag and 

go back to the server to get the image and we are simply using 

the DLL to return what could be a dynamic image.



1. Use a page producer with the following for the HTMLDoc 

property:



<html>

<body>

This is a test<BR>

<img src="/scripts/mydll.dll/picture">

</body>

</html>



2. Now set up an action with the PathInfo of /picture and return the

following:



(make sure your app "uses" the PJEG unit.



procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;

  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);

var

  Jpg: TJpegImage;

  S: TMemoryStream;

begin

  Jpg := TJpegImage.Create;

  try

    Jpg.LoadFromFile('test');

    S := TMemoryStream.Create;

    try

      Jpg.SaveToStream(S);

      S.Position := 0;

      Response.ContentType := 'image/jpeg';

      Response.ContentStream := S;

      // Must be done prior to freeing the stream

      Response.SendResponse;  

    finally

      S.Free;

    end;

  finally

    Jpg.Free;

  end;

end;




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile