### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to convert from TBGRABitmap to TBitmap?  (Read 690 times)

#### domasz

• Full Member
• Posts: 232
##### How to convert from TBGRABitmap to TBitmap?
« on: February 22, 2023, 12:42:18 pm »
I thought this should work but it just creates a full black image.

I know I can do "A.Assign(B);" but I would prefer to keep whatever pixelformat and width/height I have.

Code: Pascal  [Select][+][-]
1. var A: TBitmap;
2.     B: TBGRABitmap;
3. begin
4.   B := TBGRABitmap.Create('test.png');
5.
6.   A := TBitmap.Create;
7.   A.PixelFormat := pf32bit;
8.   A.Width := B.Width;
9.   A.Height := B.Height;
10.
11.
12.   B.Draw(A.Canvas, 0,0);
13.   A.SaveToFile('test.bmp');
14.   A.Free;
15.   B.Free;
« Last Edit: February 22, 2023, 12:48:16 pm by domasz »

#### KodeZwerg

• Hero Member
• Posts: 1399
##### Re: How to convert from TBGRABitmap to TBitmap?
« Reply #1 on: February 22, 2023, 01:09:08 pm »
Code: Pascal  [Select][+][-]
1. procedure TForm1.Button1Click(Sender: TObject);
2. var
3.   A: TBitmap;
4.   B: TBGRABitmap;
5. begin
6.   B := TBGRABitmap.Create(ExtractFilePath(ParamStr(0)) + 'KodeZwerg.png');
7.   try
8.     A := TBitmap.Create;
9.     try
10.       A.PixelFormat := pf32bit; // it automatic be whatever your display got, it is bugged
11.       A.Width := B.Width;
12.       A.Height := B.Height;
13.       A.Canvas.CopyMode := cmSrcCopy;
14.       A.Canvas.CopyRect(
15.             Rect(0,
16.                  0,
17.                  A.Width,
18.                  A.Height),
19.             B.Canvas,
20.             Rect(0,
21.                  0,
22.                  B.Width,
23.                  B.Height));
24.       A.SaveToFile(ExtractFilePath(ParamStr(0)) + 'KodeZwerg.bmp');
25.     finally
26.       A.Free;
27.     end;
28.   finally
29.     B.Free;
30.   end;
31. end;

« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

#### domasz

• Full Member
• Posts: 232
##### Re: How to convert from TBGRABitmap to TBitmap?
« Reply #2 on: February 22, 2023, 01:10:31 pm »

Interesting, thank you!

#### lainz

• Hero Member
• Posts: 4150
##### Re: How to convert from TBGRABitmap to TBitmap?
« Reply #3 on: March 21, 2023, 11:37:24 pm »
The first spammer that uses Chat-GPT? Or just did a search... Casino link.

#### KodeZwerg

• Hero Member
• Posts: 1399
##### Re: How to convert from TBGRABitmap to TBitmap?
« Reply #4 on: March 21, 2023, 11:52:53 pm »
I know I can do "A.Assign(B);"
bmp.Assign(bgraBmp.Bitmap);
response is good
I am curious how this can be good when it was already in original post but please do what you think is best for forum
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

#### lainz

• Hero Member
• Posts: 4150
##### Re: How to convert from TBGRABitmap to TBitmap?
« Reply #5 on: March 22, 2023, 12:14:52 am »
Yes, but keep it is not as bad, at lease shows with actual code how to assign..

You can do this as well:

Code: Pascal  [Select][+][-]
1. BGRAReplace(bgra, bgra.resample(bitmap.width, bitmap.height));

To resample with high quality the bgrabitmap.

Then you can draw it to any canvas:

Code: Pascal  [Select][+][-]
1. bgra.Draw(bitmap.canvas, 0, 0);