Recent

Author Topic: draw an image or a bitmap in memory and modify it in memory  (Read 1849 times)

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
draw an image or a bitmap in memory and modify it in memory
« on: April 20, 2021, 03:33:35 pm »
please describe how to draw an image or a bitmap in memory and modify it in memory and how to put it on the screen
« Last Edit: April 20, 2021, 03:35:10 pm by rabbit_dance »

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #2 on: April 20, 2021, 03:54:22 pm »
thanks, but how to do so under console under such as linux?

Handoko

  • Hero Member
  • *****
  • Posts: 5150
  • My goal: build my own game engine using Lazarus
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #3 on: April 20, 2021, 04:32:18 pm »
Console?  :o

Why do you need doing graphics in console mode? I haven't tried but if TBitmap can be used on console mode then there is no difference as doing it in GUI mode, just don't show it on the GUI form.

Below is a demo drawing objects on TBitmap in memory (GUI mode), which with some modifications I believe can be used in console mode:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnShow: TButton;
  16.     btnCreate: TButton;
  17.     btnClear: TButton;
  18.     btnWhite: TButton;
  19.     btnCircle: TButton;
  20.     btnSquare: TButton;
  21.     procedure btnCircleClick(Sender: TObject);
  22.     procedure btnClearClick(Sender: TObject);
  23.     procedure btnCreateClick(Sender: TObject);
  24.     procedure btnShowClick(Sender: TObject);
  25.     procedure btnSquareClick(Sender: TObject);
  26.     procedure btnWhiteClick(Sender: TObject);
  27.     procedure FormCreate(Sender: TObject);
  28.   private
  29.     FBitmap: TBitmap;
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormCreate(Sender: TObject);
  42. begin
  43.   FBitmap := nil;
  44. end;
  45.  
  46. procedure TForm1.btnCircleClick(Sender: TObject);
  47. const
  48.   D = 30;
  49. var
  50.   X, Y: Integer;
  51. begin
  52.   if not(Assigned(FBitmap)) then
  53.   begin
  54.     ShowMessage('Please create the bitmap first.');
  55.     Exit;
  56.   end;
  57.   X := Random(FBitmap.Width  - D);
  58.   Y := Random(FBitmap.Height - D);
  59.   FBitmap.Canvas.Brush.Color := Random($FFFFFF);
  60.   FBitmap.Canvas.Ellipse(X, Y, X+D, Y+D);
  61. end;
  62.  
  63. procedure TForm1.btnClearClick(Sender: TObject);
  64. begin
  65.   if not(Assigned(FBitmap)) then
  66.   begin
  67.     ShowMessage('Please create the bitmap first.');
  68.     Exit;
  69.   end;
  70.   FBitmap.Canvas.Brush.Color := clBlack;
  71.   FBitmap.Canvas.Clear;
  72. end;
  73.  
  74. procedure TForm1.btnShowClick(Sender: TObject);
  75. begin
  76.   if not(Assigned(FBitmap)) then
  77.   begin
  78.     ShowMessage('The bitmap has not created yet.');
  79.     Exit;
  80.   end;
  81.   Canvas.Draw(10, 10, FBitmap)
  82. end;
  83.  
  84. procedure TForm1.btnSquareClick(Sender: TObject);
  85. const
  86.   S = 30;
  87. var
  88.   X, Y: Integer;
  89. begin
  90.   if not(Assigned(FBitmap)) then
  91.   begin
  92.     ShowMessage('Please create the bitmap first.');
  93.     Exit;
  94.   end;
  95.   X := Random(FBitmap.Width  - S);
  96.   Y := Random(FBitmap.Height - S);
  97.   FBitmap.Canvas.Brush.Color := Random($FFFFFF);
  98.   FBitmap.Canvas.Rectangle(X, Y, X+S, Y+S);
  99. end;
  100.  
  101. procedure TForm1.btnWhiteClick(Sender: TObject);
  102. begin
  103.   if not(Assigned(FBitmap)) then
  104.   begin
  105.     ShowMessage('Please create the bitmap first.');
  106.     Exit;
  107.   end;
  108.   FBitmap.Canvas.Brush.Color := clWhite;
  109.   FBitmap.Canvas.Rectangle(0, 0, FBitmap.Width, FBitmap.Height);
  110. end;
  111.  
  112. procedure TForm1.btnCreateClick(Sender: TObject);
  113. begin
  114.   if Assigned(FBitmap) then
  115.   begin
  116.     ShowMessage('The bitmap has been created.');
  117.     Exit;
  118.   end;
  119.   FBitmap := TBitmap.Create;
  120.   FBitmap.Height := 100;
  121.   FBitmap.Width  := 200;
  122. end;
  123.  
  124. end.

For your information, doing image using TCanvas is slow. If you want to develop a 'real' graphics program (maybe batch editing, watermarking, or resizing) you should use a 'better' graphics library.

https://wiki.lazarus.freepascal.org/Graphics_libraries

[edit]
Oops, I forgot. We should free the FBitmap before exiting the program.
« Last Edit: April 20, 2021, 04:48:22 pm by Handoko »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #4 on: April 20, 2021, 06:04:25 pm »
thanks, but how to do so under console under such as linux?

Here is an example.

rabbit_dance

  • Full Member
  • ***
  • Posts: 157
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #5 on: April 20, 2021, 06:16:26 pm »
thanks, but how to do so under console under such as linux?

Here is an example.
the problem discussed in your post is not same to my problem.
the code in the given example contains unit graphics, which does not contain in rtl and fcl.
« Last Edit: April 20, 2021, 06:19:04 pm by rabbit_dance »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #6 on: April 20, 2021, 06:21:21 pm »
You know better what you need. The example gives you a canvas to draw on, that's all.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: draw an image or a bitmap in memory and modify it in memory
« Reply #7 on: April 20, 2021, 06:29:23 pm »
I just saw your other thread and now I see what you mean.

 

TinyPortal © 2005-2018