Recent

Author Topic: Question about Tbitmap and TRawImage updates  (Read 774 times)

jamie

  • Hero Member
  • *****
  • Posts: 7323
Question about Tbitmap and TRawImage updates
« on: August 03, 2025, 08:28:24 pm »
Hello, it's me again !

 I'll try to explain this as best as I can as to how I have figured out to get what I need.

 First of all.

 I created a Tbitmap, set the pixelformat to 32, Set the size to 320x256;
 
 All of that is working great.

 I do a FillRect and clear the surface with a specific color, for this case lets say clBlue;

 That works good too, then following, I directly scan the RawImage and set Alpa channel so that I can have a blended image and this is where things go a little off the rocks.

 What I found is whatever I do with the CANVAS..... prior to directly working with the RawImage, the raw Image will get updated as I would expect.

 When I get ready to draw this to a canvas, lets say the FORM, I use the Canvas.Draw(0,0, MyImage);
 
  That works however, any changes I made in the RawImage prior to that and after the clear canvans does not show.

so this is the order

Create the Image
Set the Pixel size = ps32bit;
Set the Image Size = 320x256
Set the Canvas Brush color = clBlue;
Use the Canvas FillRect etc;
Scan the RawImage and adjust the Alpha channel to $02 for instead but don't touch any of the color channels

Then I draw the image using Form's canvas.Draw(0,0, MyImage);

It seems the Tbitmap is making a GUI copy of the raw image whenever any of the Canvas items get called and after that, any changes to the Raw Image does not reflect in the GUI.

So what I Did was call the "ReleaseBitmapHandle" prior to painting to the screen and that recreates the image from what I can see and works

 My question is, does this properly adjust the resources ?

Jamie


 
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7323
Re: Question about Tbitmap and TRawImage updates
« Reply #1 on: August 03, 2025, 09:12:26 pm »
I had to get on a different computer to post the test example.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, StdCtrls,lclType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. Var
  34.   B:TBitmap;
  35.   X,Y:Integer;
  36. begin
  37.  //  lclType.TRGBAQuad
  38.   B := Tbitmap.Create;
  39.   B.Pixelformat := pf32bit;
  40.   B.SetSize(320,256);
  41.   B.Canvas.Brush.Color := clBlue;
  42.   B.Canvas.FillRect(0,0,319,255);
  43.  For Y := 0 to 255 do
  44.    For X := 0 to 319 do
  45.      With
  46.     PRGBAQuad(B.RawImage.GetLineStart(Y))[X] Do
  47.    Begin
  48.      Alpha :=$1;
  49.     // Green := 0;
  50.     // Red := $00;
  51.     //Blue := 255;
  52.    end;
  53.   B.ReleaseBitmapHandle; //Need to force a GDI reload.
  54.   Canvas.Draw(0,0,B);
  55.   B.Free;
  56. end;
  57.  
  58. end.
  59.  
  60.  
  61.  

This is what I need to do to force a resync from the raw image.
is this proper or do I need something else ?

 I am trying to keep this simple because in the end, I use compiler conditions to switch the code between Laz and Delphi, in Delphi I don't do it this way.

Jamie
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13226
Re: Question about Tbitmap and TRawImage updates
« Reply #2 on: August 03, 2025, 11:56:55 pm »
This reminds me of a line somewhere related to ScanLine which should be used only inside a BeginUpdate/EndUpdate block. And this works here too:

Code: Pascal  [Select][+][-]
  1.   B.BeginUpdate;
  2.   For Y := 0 to B.Height-1 do
  3.     For X := 0 to B.Width-1 do
  4.       With PRGBAQuad(B.RawImage.GetLineStart(Y))[X] Do
  5.       Begin
  6.         Alpha := $3F;
  7.       end;
  8.   B.EndUpdate;
  9.   // B.ReleaseBitmapHandle; //Need to force a GDI reload.

jamie

  • Hero Member
  • *****
  • Posts: 7323
Re: Question about Tbitmap and TRawImage updates
« Reply #3 on: August 04, 2025, 01:41:38 am »
This reminds me of a line somewhere related to ScanLine which should be used only inside a BeginUpdate/EndUpdate block. And this works here too:

Code: Pascal  [Select][+][-]
  1.   B.BeginUpdate;
  2.   For Y := 0 to B.Height-1 do
  3.     For X := 0 to B.Width-1 do
  4.       With PRGBAQuad(B.RawImage.GetLineStart(Y))[X] Do
  5.       Begin
  6.         Alpha := $3F;
  7.       end;
  8.   B.EndUpdate;
  9.   // B.ReleaseBitmapHandle; //Need to force a GDI reload.

I could of sworn I tried that  before ?

I just tried it again and it seems to work, Thanks. That makes more sense.

One thing I do noticed however. sometimes when I clear the canvas using the Canvas.FillRect... The Alpha channel seems to get set to 0, instead of $FF. It seems to be random. I will need to keep my eye on that one.

Thanks.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018