Recent

Author Topic: How to create and return PNG image using HTTP-server?  (Read 935 times)

artem101

  • Jr. Member
  • **
  • Posts: 84
How to create and return PNG image using HTTP-server?
« on: December 26, 2020, 03:49:00 pm »
Is there an example how to draw and return PNG image using fpWeb module?
Thanks.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to create and return PNG image using HTTP-server?
« Reply #1 on: December 26, 2020, 04:02:00 pm »
There might be but I don't know of any. However, it shouldn't be too difficult: use a TPicture or directly a TPortableNetworkGraphic to draw whatever you need, save it to a file/stream and make the server return that file/stream.

You could even transfer directly the TPicture/TPortableNetworkGraphic  to the Response.ContentStream using SaveToStream, avoiding all iteraction with the disk (other than OS swapping, o.c.)

Edit: Replaced TPNGImage by TPortableNetworkGraphic 
« Last Edit: December 26, 2020, 04:06:47 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: How to create and return PNG image using HTTP-server?
« Reply #2 on: December 26, 2020, 04:37:48 pm »
I make this code, but it doesn`t work:
Code: Pascal  [Select][+][-]
  1. procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  2.   AResponse: TResponse; var Handled: Boolean);
  3. const
  4.   WIDTH = 256;
  5.   HEIGHT = 256;
  6. var
  7.   str: String;
  8.   img: TPortableNetworkGraphic;
  9.   textWidth, textHeight: integer;
  10.   memStrm: TMemoryStream;
  11. begin
  12.   //WriteLn('Url: ', ARequest.URI);
  13.   //WriteLn('Args: ', ARequest.QueryFields.Text);
  14.  
  15.   with ARequest.QueryFields do
  16.     str := Format('x=%s y=%s z=%s', [Values['x'], Values['y'], Values['z']]);
  17.  
  18.  
  19.   img := TPortableNetworkGraphic.Create;
  20.   memStrm := TMemoryStream.Create;
  21.   try
  22.     // Create image
  23.     img.SetSize(WIDTH, HEIGHT);
  24.  
  25.     img.Canvas.Font.Color:=clRed;
  26.     img.Canvas.GetTextSize(str, textWidth, textHeight);
  27.     img.Canvas.TextOut((WIDTH - textWidth) div 2, (HEIGHT - textHeight) div 2, str);
  28.  
  29.     // Send image to output stream
  30.     img.SaveToStream(memStrm);
  31.     AResponse.ContentStream.CopyFrom(memStrm, memStrm.Size);
  32.     AResponse.ContentStream.Position:=0;
  33.     AResponse.WriteContent;
  34.   finally
  35.     img.Free;
  36.     AResponse.ContentStream:=nil;
  37.     memStrm.Free;
  38.   end;
  39.  
  40.   AResponse.Code:=200;
  41.   AResponse.ContentType:='image/png';
  42.   Handled:=true;
  43. end;  

First problem:
Quote
unit1.pas(64,15) Error: identifier idents no member "WriteContent"

After I commented line
Code: Pascal  [Select][+][-]
  1. AResponse.WriteContent;
server started to work, but second problem appeared
https://ibb.co/6gbHSn5
at line
Code: Pascal  [Select][+][-]
  1. img.SetSize(WIDTH, HEIGHT);

 

TinyPortal © 2005-2018