Recent

Author Topic: [SOLVED] Image Canvas Save  (Read 926 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 547
[SOLVED] Image Canvas Save
« on: March 13, 2023, 07:07:43 pm »
Hello forum users, I have a TImage component on the form, a picture in it, I write text on the canvas, how can I save the whole thing to a file?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   with Image1.Canvas do
  4.   begin
  5.     Font.Color := clBlack;
  6.  
  7.     Font.Size := 13;
  8.     Brush.Style := bsClear;
  9.  
  10.     Refresh;
  11.     Repaint;
  12.     TextOut(33, 13, Edit1.Text);
  13.   end;
  14. end;
  15.  
« Last Edit: March 15, 2023, 07:46:58 pm by Pe3s »

Pe3s

  • Hero Member
  • *****
  • Posts: 547
Re: Image Canvas Save
« Reply #1 on: March 13, 2023, 09:54:53 pm »
If I change to Image1.Picture.Bitmap.Canvas, the text becomes gray and illegible.
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Edit1Change(Sender: TObject);
  2.     begin
  3.       with Image1.Picture.Bitmap.Canvas do
  4.       begin
  5.         Font.Color := clBlack;
  6.      
  7.         Font.Size := 13;
  8.         Brush.Style := bsClear;
  9.      
  10.         Refresh;
  11.         Repaint;
  12.         TextOut(33, 13, Edit1.Text);
  13.       end;
  14.     end;
  15.      
  16.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Image Canvas Save
« Reply #2 on: March 13, 2023, 10:32:31 pm »
Here's one possible way:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. var
  3.   pic: TPicture = Nil;
  4. begin
  5.   Image1.Canvas.Font.Color := clBlack;
  6.   Image1.Canvas.Font.Size := 13;
  7.   Image1.Canvas.Brush.Style := bsClear;
  8.   Image1.Canvas.TextOut(33, 13, Edit1.Text);
  9.   pic := TPicture.Create;
  10.   try
  11.     pic.Bitmap.SetSize(Image1.Width, Image1.Height);
  12.     pic.Bitmap.Canvas.CopyRect(Rect(0, 0, pic.Width, pic.Height), Image1.Canvas, Rect(0, 0, Image1.Width, Image1.Height));
  13.     pic.SaveToFile('test.png');
  14.   finally
  15.     pic.Free;
  16.   end;
  17. end;
You would probably want to show an OpenDialog window to choose where to save the file, but I leave that for you to implement.

Pe3s

  • Hero Member
  • *****
  • Posts: 547
Re: Image Canvas Save
« Reply #3 on: March 14, 2023, 06:30:39 pm »
@howardpc Thank you :)
I have one more question how can I print Timage with Canvas text ?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Image Canvas Save
« Reply #4 on: March 15, 2023, 09:42:48 am »
All things are possible...
Printing or saving a file in the middle of an edit OnChange event is not good practice. So I attach a little project which shows one way to print an image overlaid with text inserted from an edit control. Printing is triggered by a button click rather than an OnChange event.
Since most printers are high resolution the text is likely to be pixellated in the printed output.

Pe3s

  • Hero Member
  • *****
  • Posts: 547
Re: Image Canvas Save
« Reply #5 on: March 15, 2023, 07:46:41 pm »
@howardpc, Thank you very much for your help  :)

 

TinyPortal © 2005-2018