Recent

Author Topic: (SOLVED) TextOut Problem  (Read 841 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2140
    • Lazarus and Free Pascal italian community
(SOLVED) TextOut Problem
« on: October 01, 2019, 10:38:22 am »
Hi guys, I have to do a web application that manipulates some images. And it seems to work well, the only thing is that when I have to write something on the canvas it goes wrong. I enclose an example in which if you comment on the line containing the TextOut you will see the correct result, otherwise not. Can anyone tell me what I'm wrong?

EDIT: this is url to view the page on browser
localhost:8090/TFPWebModule1/image
« Last Edit: October 03, 2019, 08:36:01 am by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2140
    • Lazarus and Free Pascal italian community
Re: TextOut Problem
« Reply #1 on: October 02, 2019, 09:45:11 am »
No suggestion? For those who downloaded it, can you replicate the error?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

paweld

  • Full Member
  • ***
  • Posts: 249
Re: TextOut Problem
« Reply #2 on: October 02, 2019, 04:50:15 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  
  9.     SysUtils, Classes, httpdefs, fpHTTP, fpWeb,
  10.     Graphics,
  11.     fpimage, FPCanvas, FPImgCanv,
  12.     FPWriteJPEG,
  13.     FTFont;
  14.  
  15. type
  16.  
  17.   { TFPWebModule1 }
  18.  
  19.   TFPWebModule1 = class(TFPWebModule)
  20.     procedure imageRequest(Sender: TObject; ARequest: TRequest;
  21.       AResponse: TResponse; var Handled: Boolean);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   FPWebModule1: TFPWebModule1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TFPWebModule1 }
  36.  
  37. procedure TFPWebModule1.imageRequest(Sender: TObject; ARequest: TRequest;
  38.   AResponse: TResponse; var Handled: Boolean);
  39. var canvas    : TFPcustomCanvas;
  40.     image     : TFPCustomImage;
  41.     writer    : TFPCustomImageWriter;
  42.     MyStream  : TMemoryStream;
  43.     img_src   : string;
  44.     AFont: TFreeTypeFont;
  45. begin
  46.  
  47.      img_src := ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'img.jpg';
  48.  
  49.      if FileExists(img_src) then
  50.      begin
  51.        AResponse.ContentType := 'image/jpeg';
  52.  
  53.        FTFont.InitEngine;
  54.        FontMgr.SearchPath := ExtractFilePath(ParamStr(0));
  55.        AFont := TFreeTypeFont.Create;
  56.  
  57.        image := TFPMemoryImage.Create (0,0);
  58.        image.LoadFromFile(img_src);
  59.  
  60.        Canvas := TFPImageCanvas.Create (image);
  61.        Writer := TFPWriterJPEG.Create;
  62.  
  63.        Canvas.Font:=AFont;
  64.        Canvas.Font.Name:='Arial';
  65.  
  66.        Canvas.Brush.FPColor := TColorToFPColor(clWhite);
  67.        Canvas.Font.FPColor  := TColorToFPColor(clYellow);
  68.        Canvas.Font.Size     := 18;
  69.  
  70.  
  71.        Canvas.TextOut(20, 40,'Hello world'); //Comment this line to run correctly
  72.  
  73.  
  74.  
  75.        MyStream := TMemoryStream.Create;
  76.        image.SaveToStream(MyStream, writer);
  77.        AResponse.ContentStream := MyStream;
  78.  
  79.  
  80.        Canvas.Free;
  81.        image.Free;
  82.        writer.Free;
  83.        AFont.Free;
  84.      end else begin
  85.        AResponse.ContentType := 'text/html';
  86.        AResponse.Contents.Text:='Image not found : ' + img_src;
  87.      end;
  88.  
  89.      Handled := true;
  90. end;
  91.  
  92. initialization
  93.   RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
  94. end.
  95.  
Best regards
paweld

xinyiman

  • Hero Member
  • *****
  • Posts: 2140
    • Lazarus and Free Pascal italian community
Re: TextOut Problem
« Reply #3 on: October 03, 2019, 08:35:49 am »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  
  9.     SysUtils, Classes, httpdefs, fpHTTP, fpWeb,
  10.     Graphics,
  11.     fpimage, FPCanvas, FPImgCanv,
  12.     FPWriteJPEG,
  13.     FTFont;
  14.  
  15. type
  16.  
  17.   { TFPWebModule1 }
  18.  
  19.   TFPWebModule1 = class(TFPWebModule)
  20.     procedure imageRequest(Sender: TObject; ARequest: TRequest;
  21.       AResponse: TResponse; var Handled: Boolean);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   FPWebModule1: TFPWebModule1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TFPWebModule1 }
  36.  
  37. procedure TFPWebModule1.imageRequest(Sender: TObject; ARequest: TRequest;
  38.   AResponse: TResponse; var Handled: Boolean);
  39. var canvas    : TFPcustomCanvas;
  40.     image     : TFPCustomImage;
  41.     writer    : TFPCustomImageWriter;
  42.     MyStream  : TMemoryStream;
  43.     img_src   : string;
  44.     AFont: TFreeTypeFont;
  45. begin
  46.  
  47.      img_src := ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'img.jpg';
  48.  
  49.      if FileExists(img_src) then
  50.      begin
  51.        AResponse.ContentType := 'image/jpeg';
  52.  
  53.        FTFont.InitEngine;
  54.        FontMgr.SearchPath := ExtractFilePath(ParamStr(0));
  55.        AFont := TFreeTypeFont.Create;
  56.  
  57.        image := TFPMemoryImage.Create (0,0);
  58.        image.LoadFromFile(img_src);
  59.  
  60.        Canvas := TFPImageCanvas.Create (image);
  61.        Writer := TFPWriterJPEG.Create;
  62.  
  63.        Canvas.Font:=AFont;
  64.        Canvas.Font.Name:='Arial';
  65.  
  66.        Canvas.Brush.FPColor := TColorToFPColor(clWhite);
  67.        Canvas.Font.FPColor  := TColorToFPColor(clYellow);
  68.        Canvas.Font.Size     := 18;
  69.  
  70.  
  71.        Canvas.TextOut(20, 40,'Hello world'); //Comment this line to run correctly
  72.  
  73.  
  74.  
  75.        MyStream := TMemoryStream.Create;
  76.        image.SaveToStream(MyStream, writer);
  77.        AResponse.ContentStream := MyStream;
  78.  
  79.  
  80.        Canvas.Free;
  81.        image.Free;
  82.        writer.Free;
  83.        AFont.Free;
  84.      end else begin
  85.        AResponse.ContentType := 'text/html';
  86.        AResponse.Contents.Text:='Image not found : ' + img_src;
  87.      end;
  88.  
  89.      Handled := true;
  90. end;
  91.  
  92. initialization
  93.   RegisterHTTPModule('TFPWebModule1', TFPWebModule1);
  94. end.
  95.  

Thank you very much
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018