Recent

Author Topic: duplicate tbitmap how?  (Read 2527 times)

Josh

  • Hero Member
  • *****
  • Posts: 1274
duplicate tbitmap how?
« on: January 10, 2022, 05:43:10 pm »
Hi

Apologies my brain is having a non working day :)

How do i duplicate tbitmaps ie bitmap1:=bitmap2 .

I can create the bitmap1, then i can assign it to timage  to make sure it works.

i need to then copy the bitmap1 to bitmap2, and then modify bitmap 2.

i have tried using bitmap2.canvas.copyrect(rect(0,0,width,height),bitmap1,rect(...))
but this does not duplicate the contents,
if i assign bitmap2 to timage2, it is empty.

Any ideas?

its to create a fixed bitmap (bitmap1); which then gets updated/changed in bitmap2 to update a timage, like a buffer
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: duplicate tbitmap how?
« Reply #1 on: January 10, 2022, 08:10:33 pm »
Hi!

Very simple, step by step:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClick(Sender: TObject);
  2. var bmp1,bmp2 : TBitmap;
  3. R : TRect;
  4. begin
  5. bmp1 := TBitmap.create;
  6. bmp1.setsize (200,100);
  7. R := Rect(0,0,bmp1.Width,bmp1.height);
  8. bmp1.Canvas.Brush.Color := clWhite;
  9. bmp1.Canvas.Pen.Color := clRed;
  10. bmp1.canvas.FillRect(R);
  11. bmp1.canvas.Line(R);
  12.  
  13. bmp2 := TBitmap.create;
  14. bmp2.SetSize(200,100);
  15. bmp2.Canvas.CopyRect(R,bmp1.Canvas,R);
  16.  
  17. canvas.copyRect (R,bmp2.canvas,R);
  18. bmp1.free;
  19. bmp2.free;
  20. end;

Winni

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: duplicate tbitmap how?
« Reply #2 on: January 11, 2022, 12:32:41 am »
Hi Winni,

Thanks for that,it un froze my brain...  Back on track now.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: duplicate tbitmap how?
« Reply #3 on: January 28, 2022, 01:14:19 am »
Also you can use TBitmap.Assign — the result will be the same (exact copy of the source bitmap object).
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018