Recent

Author Topic: Assigning a TBitmap to a TBitmap32  (Read 7659 times)

niotronic

  • New Member
  • *
  • Posts: 13
Assigning a TBitmap to a TBitmap32
« on: May 16, 2021, 12:38:36 pm »
Hi,

I'm using the TImage32, which is part of the graphics32 library and I'm trying to assign a TBitmap to the TImage32.Bitmap, which always results in a totally black screen. On the canvas of the TBitmap32 (=TImage32.Bitmap) however I can draw lines and fill rects without problems. If I assign the TBitmap to a TImage.Bitmap (the standard TImage) the bitmap contained in the original Bitmap is shown correctly. So it's obviously a problem with the TImage32.Bitmap not accepting the standard TBitmap contents ?

Code: Pascal  [Select][+][-]
  1.  
  2. FImage:=TImage32.Create(self);
  3. FImage.Parent:=self;
  4. FImage.Align:=alClient;
  5. FImage.Scale:=1;
  6.  
  7. FImage.Bitmap.Assign(Bitmap);    //shows up as a black screen
  8. FImage.Bitmap.LoadFromFile('/home/arm1/Dokumente/Linux NIO Analyzer  V2-90 - FPC/Graphics/TDIBackground.bmp');    //also shows up as a black screen
  9. FImage.Bitmap.Changed;
  10.  
  11. FImage.Bitmap.Clear(cllime);     //this is shown correctly
  12. FImage.Bitmap.Line(0, 0, 150, 150, clblack, TRUE);  //shown correctly
  13.  
  14.  

Any ideas, what might cause the TImage32 to show any Bitmap assigned as a black screen ?

Best regards

KL

winni

  • Hero Member
  • *****
  • Posts: 2402
Re: Assigning a TBitmap to a TBitmap32
« Reply #1 on: May 16, 2021, 01:11:49 pm »
Hi!

a)

Code: Pascal  [Select][+][-]
  1. FImage.LoadFromFile('/home/....');

b)
There are a lot of examples in

Code: Text  [Select][+][-]
  1. ct4laz/pl_examples/pl_graphics32/

Winni

niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #2 on: May 16, 2021, 07:57:20 pm »
Dear Winni,

your suggestion a) is unfortunately not applicable for me since I'm loading the image out of a database via a TStream.

The same code works pretty fine on DELPHI, but when I compile on Lazarus, the TImage32 always shows a black screen after a calling:

FImage.Assign(MyBitmap);

When I for test purposes only assign MyBitmap to a standard TImage it correctly shows the contents of MyBitmap, which prooves that MyBitmap contains correct data.

On the otherhand I can draw on the TImage32 canvas whatever I want and it shows it correctly.

So the problem must be anywhere in the

FImage.Assign(myBitmap)  which obviously cannot deal with the data contained in Mybitmap.

Just to remember: This is working pretty fine on DELPHI...

I'm using Lazarus 1.8.4 and the graphics32 version is 1.9

Any call of FImage.Changed, FImage.ForceFullInvalidate etc. doesn't make it show the contents of MyBitmap...

Best regards,

 KL


engkin

  • Hero Member
  • *****
  • Posts: 2812
Re: Assigning a TBitmap to a TBitmap32
« Reply #3 on: May 16, 2021, 08:51:31 pm »
The following code works correctly here:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   FImage: TImage32;
  4.   Bitmap: TBitmap;
  5. begin
  6.  FImage:=TImage32.Create(self);
  7.  FImage.Parent:=self;
  8.  FImage.Align:=alClient;
  9.  FImage.Scale:=1;
  10.  
  11.  Bitmap := TBitmap.Create;
  12.  Bitmap.LoadFromFile('c:\Path\To\Some.bmp');
  13.  FImage.Bitmap.Assign(Bitmap);    //Works
  14.  

I am using Win32, Laz2.0.10, GR32 2.0

niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #4 on: May 20, 2021, 06:29:57 pm »
I tried now to use the latest version of Lazarus (2.1) and of the graphics32 (V1.9 fixed) but unfortunately there hasn't anything changed.
Calling the Assign procedure of TImage32 with a TBitmap as Parameter still results in a black screen - However directly drawing on the Bitmap of the TImage32 works perfectly.
I wrote a small test program which shows this problem...
The whole project is attached.

Attached are also screenshots, one them shows the black screen after pressing "Assign bitmap" - The standard TImage shows the picture correctly, the TImage32 only shows a black screen.
The results of clicking on "Draw on TImage32.Bitmap" is shown in the second picture...


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, GR32_Image,
  9.   ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Image1: TImage;
  19.     Panel1: TPanel;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure Button2Click(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormDestroy(Sender: TObject);
  24.   private
  25.     FImage32: TImage32;
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.Button1Click(Sender: TObject);
  40. var ABitmap: TBitmap;
  41. begin
  42. ABitmap:=TBitmap.create;
  43. try
  44. ABitmap.LoadFromFile('/home/arm1/Downloads/Floorplan2.bmp');
  45. FImage32.Bitmap.Assign(ABitmap);
  46. Image1.Picture.Bitmap.Assign(ABitmap);
  47. finally
  48.   ABitmap.Free;
  49. end;
  50.  
  51. //FImage32.Bitmap.LoadFromFile('/home/arm1/Downloads/Floorplan2.bmp');
  52.  
  53. end;
  54.  
  55. procedure TForm1.Button2Click(Sender: TObject);
  56. begin
  57.   FImage32.Bitmap.Clear(cllime);
  58.   FImage32.Bitmap.Line(10, 10, 200, 200, clblack);
  59.   FImage32.Bitmap.FillRect(140, 40, 180, 80, clblue);
  60. end;
  61.  
  62. procedure TForm1.FormCreate(Sender: TObject);
  63. begin
  64.  FImage32:=TImage32.Create(self);
  65.  FImage32.Align:=alClient;
  66.  FImage32.Parent:=self;
  67. end;
  68.  
  69. procedure TForm1.FormDestroy(Sender: TObject);
  70. begin
  71.   FImage32.Free;
  72. end;
  73.  
  74. end.
  75.  




niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #5 on: May 20, 2021, 06:40:58 pm »
Sorry - I forgot to say that the black screen only shows up on Linux X86-64 and Lazarus. The same source code works fine on Windows / DELPHI 10.3...

winni

  • Hero Member
  • *****
  • Posts: 2402
Re: Assigning a TBitmap to a TBitmap32
« Reply #6 on: May 20, 2021, 07:09:08 pm »

niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #7 on: May 21, 2021, 09:46:54 am »
Dear Winni,

the problem discussed at https://forum.lazarus.freepascal.org/index.php?topic=49913.0 is talking about the same problem, but the solutions provided is using a TImage which a TBitmap32 is assigned to. It's not shoing a solutions to the problem with TImage32.bitmap.Assign(Mybitmap).

However I need to use a TImage32 and I'm assining a standard TBitmap.
I did some more tests: Even if I'm using Timage32.Bitmap.LoadFromFile it's only showing a black screen...


niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #8 on: May 21, 2021, 12:31:58 pm »
Addendum:
It seems to be a problem with the bitmap format.
When I manually draw something on the TImage32.Bitmap and save this to a *.BMP file I can later on read and show the contents of this file.

Code: Pascal  [Select][+][-]
  1.   FImage:=TImage32.Create(self);
  2.   FImage.Parent:=self;
  3.   FImage.Align:=alClient;
  4.   FImage.ScaleMode:=smStretch;
  5.  
  6.  // FImage.Bitmap.Assign(Bitmap);
  7.  
  8.   FImage.Bitmap.SetSize(600, 400);
  9.   FImage.Bitmap.Clear(cllime32);
  10.   FImage.bitmap.FillRect(10, 10, 60, 60, clblue32);
  11.   FImage.Bitmap.Line(20, 50, 40, 180, clyellow32);
  12.   FImage.Bitmap.FillRect(120, 120, 160, 160, clgreen32);
  13.   FImage.Bitmap.SaveToFile('/home/arm3/Dokumente/Test.bmp', FALSE);
  14.  
  15.   FImage.Bitmap.Clear;
  16.   FImage.Bitmap.LoadFromFile('/home/arm3/Dokumente/Test.bmp');
  17.  

However it obviously swaps the red and blue color channels. Anything that was red before saving to a  *.BMP shows up in blue, anything that was blue shows up in red when it's read from the file...

Does the Timage32.Bitmap use a different internal format ?


winni

  • Hero Member
  • *****
  • Posts: 2402
Re: Assigning a TBitmap to a TBitmap32
« Reply #9 on: May 21, 2021, 01:32:13 pm »
Hi!

As I told you above for loadFromFile do the same with SaveToFile:

Code: Pascal  [Select][+][-]
  1. var fImage : TImage32;
  2.  
  3. .....
  4. fImage.LoadFromFile;
  5. ...
  6. fImage.SaveToFile;
  7.  

If you don't read the answers then you will always ask the same questions.

Winni


niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #10 on: May 21, 2021, 04:20:28 pm »
Dear Winni,

I think we are not talking about the same thing.

First - the original problem was

to Assign a bitmap to Timage32.Bitmap

This is working perfectly on WIN32 with DELPHI.

When I'm running the same code on X86 Linux with Lazarus its resulting in a black screen.
Then started (only to investigate the problem further) with the LoadfromFile and SavetoFile functions:
Result: TImage32  does not show a picture when I call TImage32.Bitmap.Loadfrom file - it's always a black screen.

When I draw something on the TImage32 and Save it to a file then it can be read and shows correctly when I read it again with LoadFromFile

So whats different from any arbitrary *.BMP file compared the *.BMP files which were saved  with the TImage32.SaveToFile procedure ?


But I cannot use LoadfromFile since my picture are read from a Database via a TStream object.

So the one and only question is:

How to assign a TBitmap to the Bitmap32 of a TImage32 and make the Timage32 show the picture

On WIN32 / DELPHI - works like a charm
On X86 Linux /Lazarus - always shows a black screen

Best regards,

KL




lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: Assigning a TBitmap to a TBitmap32
« Reply #11 on: May 21, 2021, 04:46:10 pm »
Note that wherever you can SaveTo-/LoadFromFile, there is normally also a SaveTo-/LoadFromStream (which in fact is where saving to and loading from file is actually done, using a TFileStream) so you can omit all the file shenanigans and use the stream read from the database directly.

Though I'm noot sure whether that will work or not, seeing how it's failing with a file. :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 2402
Re: Assigning a TBitmap to a TBitmap32
« Reply #12 on: May 21, 2021, 05:40:29 pm »
Hi!

I don't get the reason for assigning TBitmap <--> TBitmap32

TBitmap32 is able to handle Transparency.
TBitmap cannot do that.

So whatever you do - you loose the transparency.
Transparency is one of the jobs TBitmap32 can do in opposite to TBitmap.

You cannot assign a cow and hope that it is now a horse.

Winni

niotronic

  • New Member
  • *
  • Posts: 13
Re: Assigning a TBitmap to a TBitmap32
« Reply #13 on: May 21, 2021, 05:59:18 pm »
Dear Winni,

I'm loading a standard bitmap file out of the database and I want to use this bitmap as the background in a TImage32. I do not want the background picture to be transparent but all the Timager32.layers which I want to show in front of the Background picture.

The problem is that obvisouly the graphics32 library is working as expected on Win32 / DELPHI - I have been using it for years now, but when I started  using it on Linux / Lazarus the TImage32 always shows a black screen - No matter which way I try to load a picture (Assign from a Bitmap, LoadfromFile, Loadfromstream etc. ) it always shows a black screen, and when I manually draw something on the Timage32 (by the use of Fillrect, line arc procedures etc.),  save the Timage32 to a file (TImage32.Bitmap.SavetoFile) and then load it again (TImage32.Bitmap.Loadfromfile) the blue and the red color channels are swapped (a red rectangle before saving show up in blue after Loadfromfile and vice versa)
This is definitely not happening on WIN32 / DELPHI.

I haven't found out yet in which library it's not implemented correctly on the Lazarus / Linux / graphics32, but I will continue to investigate what's causing this malfunctioning with Lazarus / Linux...

wp

  • Hero Member
  • *****
  • Posts: 8350
Re: Assigning a TBitmap to a TBitmap32
« Reply #14 on: May 21, 2021, 06:09:18 pm »
Can you post a small compilable project along with an image file to demonstrate the issue? It's always better to have some food for the compiler than having to imagine where the issue is.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018