Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?



Permlink Replies: 10 - Last Post: Jul 7, 2014 7:24 AM Last Post By: Benjamin Wittfoth
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 5:24 AM
Seriously, I don't get it. Lost already quite some hours with this cheap stuff. So I wrote a test program for further testing.

On the form of the test program are 2 TButton and 1 TImage components.

The critical part is the button1click method. TGenerate.GenerateImage is called which generates a random TBitmap. After that the generated TBitmap should be stored into the TImage.Bitmap directly. But it doesnt work - no image is displayed.

To validate that TBitmap is genereated I store the image into a file ('e:\image.bmp') and button2click simply loads that bitmap into a temporary TBitmap object and assigns it then to TImage - which works properly.

Why is that so? Is the original TBitmap in TGenerate.GenerateImage locked somehow? I read in the documentation that unmap finalizes the TBitmap TMapaccess manipulation process. Is there something more to do with that?

Is this a Delphi XE6 Enterprise bug? If so how can I avoid that? Using the harddrive (save and loadfromfile) isn't an option.

I would be happy if somebody can help me out here.

Thanks and best regards

unit main;
 
interface
 
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Objects;
 
const
  DEFSensors:Integer=64;
 
type
  TBitmap=FMX.Graphics.TBitmap;
 
  TGenerator=class
  private
    fImage: TBitmap;
  public
    procedure AfterConstruction;override;
    procedure GenerateImage;
    destructor destroy;override;
    property Image:TBitmap read fImage write fImage;
  end;
 
  TMainform = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    fGenerator: TGenerator;
    { Private declarations }
  public
    property Generator:TGenerator read fGenerator write fGenerator;
 
    procedure AfterConstruction;override;
    destructor destroy;override;
  end;
 
var
  Mainform: TMainform;
 
implementation
 
{$R *.fmx}
 
{ TGenerator }
 
procedure TGenerator.AfterConstruction;
begin
  inherited;
  self.fImage:=TBitmap.create;
  self.fImage.SetSize(DEFSensors,DEFSensors);
end;
 
destructor TGenerator.destroy;
begin
  self.fImage.free;
  inherited;
end;
 
procedure TGenerator.GenerateImage;
var
  _bitdata:TBitmapData;
  _X,_Y: integer;
begin
  if not(self.fImage=NIL) then begin
    self.fImage.Clear(0);
    if self.fImage.Map(TMapAccess.maReadWrite, _bitdata) then begin
      try
        for _Y := 0 to DEFSensors-1 do
          for _X := 0 to DEFSensors-1 do
            _bitdata.SetPixel(
              _x,
              _y,
              TRUNC(Random(30)*1000)
            );
      finally
        self.fImage.Unmap(_bitdata);
      end;
    end;
  end;
end;
procedure TMainform.AfterConstruction;
begin
  inherited;
  self.fGenerator:=TGenerator.Create;
end;
 
procedure TMainform.Button1Click(Sender: TObject);
var _Image:TBitmap;
begin
  self.Generator.GenerateImage;
  self.Image1.Bitmap.Assign( {doesn't work}
    self.fGenerator.Image
  );
  self.fGenerator.Image.SaveToFile('e:\image.bmp'); {but bitmap is correct}
end;
 
procedure TMainform.Button2Click(Sender: TObject);
var _Image:TBitmap;
begin
  _Image:=TBitmap.create;
  try
    _Image.LoadFromFile('e:\image.bmp');
    self.Image1.Bitmap.Assign( {works as it should}
      _Image
    );
  finally
    _Image.free;
  end;
end;
 
destructor TMainform.destroy;
begin
  self.fGenerator.free;
  inherited;
end;
end.
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 5:52 AM   in response to: Benjamin Wittfoth in response to: Benjamin Wittfoth
When I press Button1 I see every time other image.
I changed also the color for SetPixel() to $FF000000 + Random($FFFFFF) to see better.
I do not see here the dfm file. Maybe you have other issues. It will be better to post a complete project into Attachments (without executable).

Best regards,
Cristian Peta
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 6:14 AM   in response to: Cristian Peța in response to: Cristian Peța
Cristian Peta wrote:
When I press Button1 I see every time other image.
I changed also the color for SetPixel() to $FF000000 + Random($FFFFFF) to see better.
I do not see here the dfm file. Maybe you have other issues. It will be better to post a complete project into Attachments (without executable).

Best regards,
Cristian Peta

Thanks again you for your reply, Cristian.

Wow, thats great. I guess it's a Delphi XE6 Enterprise (Update 1) bug then? Which version are you using? My exact is "Embarcadero® Delphi XE6 Version 20.0.16277.1276"

I would like to upload a attachment - but how do I do that here?

But I didnt change anything - just clicked that components onto the FMX HD form. I only changed the Name Tform1 into TMainform - everything else is on standard.

Do you have any other ideas maybe? :/

Here is the code of the form.
object Mainform: TMainform
  Left = 0
  Top = 0
  ClientHeight = 700
  ClientWidth = 1114
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop, iPhone, iPad]
  DesignerMobile = False
  DesignerWidth = 0
  DesignerHeight = 0
  DesignerDeviceName = ''
  DesignerOrientation = 0
  DesignerOSVersion = ''
  object Button1: TButton
    Height = 81.000000000000000000
    Position.X = 40.000000000000000000
    Position.Y = 104.000000000000000000
    TabOrder = 1
    Text = 'Button1'
    Width = 233.000000000000000000
    OnClick = Button1Click
  end
  object Image1: TImage
    MultiResBitmap = <
      item
      end>
    Height = 313.000000000000000000
    Position.X = 400.000000000000000000
    Position.Y = 104.000000000000000000
    Width = 321.000000000000000000
  end
  object Button2: TButton
    Height = 81.000000000000000000
    Position.X = 40.000000000000000000
    Position.Y = 216.000000000000000000
    TabOrder = 3
    Text = 'Button2'
    Width = 233.000000000000000000
    OnClick = Button2Click
  end
end
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 6:35 AM   in response to: Benjamin Wittfoth in response to: Benjamin Wittfoth
I have same Delphi version.

I do not see anything unusual in dfm. To be sure there is an Attachments in the root where you can put the project. Write here after posting so I can take a look. Here is direct link:
https://forums.embarcadero.com/forum.jspa?forumID=2
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 6:41 AM   in response to: Cristian Peța in response to: Cristian Peța
Cristian Peta wrote:
I have same Delphi version.

I do not see anything unusual in dfm. To be sure there is an Attachments in the root where you can put the project. Write here after posting so I can take a look. Here is direct link:
https://forums.embarcadero.com/forum.jspa?forumID=2

Thanks again.
I've posted it there. I think it will work on your end.

Strange thing

best regards
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 6:55 AM   in response to: Benjamin Wittfoth in response to: Benjamin Wittfoth
It is working for me but after I commented out fastmm4 in the project uses clause because I do not have it (XE6 is already using FastMM)

Best regards,
Cristian Peta
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 7:05 AM   in response to: Cristian Peța in response to: Cristian Peța
Cristian Peta wrote:
It is working for me but after I commented out fastmm4 in the project uses clause because I do not have it (XE6 is already using FastMM)

Best regards,
Cristian Peta

Yes, sorry - I use fastmm for memoryleak detection. Forget to mention that but fastmm isnt causing it. When I remove it, clean the project and make a rebuild the project it still doesnt work - only button2click does it correctly.

Holy crap, that's a real blocker for me now. Maybe it's the OS? I am using Win 8 Pro.
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 7:15 AM   in response to: Benjamin Wittfoth in response to: Benjamin Wittfoth
Now I looked at saved image and is something with black and blue.
This I see when I use button2. But with button1 I see some light cyan with little light gray.
I tried to save in Jpeg format and it is now clear. You are missing Alpha channel.
Add to the color $FF000000.

Best regards,
Cristian Peta

Edited by: Cristian Peta on Jul 7, 2014 5:16 PM
typo
Cristian Peța

Posts: 157
Registered: 8/7/06
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 7:19 AM   in response to: Cristian Peța in response to: Cristian Peța
Something like:
TAlphaColorRec.Alpha + TRUNC(Random(30)*1000)
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 7:24 AM   in response to: Cristian Peța in response to: Cristian Peța
Cristian Peta wrote:
Now I looked at saved image and is something with black and blue.
This I see when I use button2. But with button1 I see some light cyan with little light gray.
I tried to save in Jpeg format and it is now clear. You are missing Alpha channel.
Add to the color $FF000000.

Best regards,
Cristian Peta

Edited by: Cristian Peta on Jul 7, 2014 5:16 PM
typo

Holy cow, that is an argument - double face palm. I am still thinking in the old 24bit triples :)

Thank you very much, Cristian!
Benjamin Wittfoth

Posts: 17
Registered: 1/7/07
Re: Assignment TBitmap to TImage.Bitmap - WT... am I missing here?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 7:14 AM   in response to: Benjamin Wittfoth in response to: Benjamin Wittfoth
Same with Delphi XE5 Enterprise - doesnt work. This is so great.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02