Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: Firemonkey XE6 assign bitmap to tImageViewer



Permlink Replies: 4 - Last Post: Jul 19, 2014 4:42 AM Last Post By: Mike Sutton
Peter Guth

Posts: 28
Registered: 2/11/05
Firemonkey XE6 assign bitmap to tImageViewer
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 17, 2014 5:30 AM
Hi,

I would like to assign a bitmap that I create programatically in memory to a TImageViewer (I have tried tImage with the same results).

This is my code, and my only working solution has been to save the bitmap to a file (so I know the bitmap is working), and then read that.

Is there a way to avoid the disk read/write operations, since the bitmap could be large?

MapDraw.DrawMapOnBMP(MyBitmap,fName); //creates an in memory bitmap

//ImageViewer1.Bitmap := MyBitmap; //does not work

//ImageViewer1.Bitmap.Assign(MyBitmap); //does not work
//ImageViewer1.Assign(MyBitmap); //does not work

MyBitmap.SaveToFile(MDTempDir + 'finmap.bmp');
ImageViewer1.Bitmap.LoadFromFile(MDTempDir + 'finmap.bmp');

Peter Guth

Eli M

Posts: 1,346
Registered: 11/9/13
Re: Firemonkey XE6 assign bitmap to tImageViewer
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 17, 2014 7:39 PM   in response to: Peter Guth in response to: Peter Guth
Use Bitmap.SetSize() first.
Peter Guth

Posts: 28
Registered: 2/11/05
Re: Firemonkey XE6 assign bitmap to tImageViewer
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 18, 2014 2:06 PM   in response to: Eli M in response to: Eli M
Use Bitmap.SetSize() first.

Thanks for the suggestion, which may well be part of the solution, but I cannot get it to work with just that. I tried:

MapDraw.DrawMapOnBMP(MyBitmap,Caption); //creates an in memory bitmap
MyBitmap.SaveToFile('c:\temp\finmap.bmp'); //bitmap works because it will save

ImageViewer1.Bitmap.SetSize(MyBitmap.Width,MyBitmap.Height);

//failure 1
ImageViewer1.Bitmap.Canvas.BeginScene;
Region.Create(0,0,pred(MyBitmap.Width),pred(MyBitmap.Height));
ImageViewer1.Bitmap.Canvas.DrawBitmap(MyBitmap,region,region,1,false);
ImageViewer1.Bitmap.Canvas.EndScene;

//previous failure 2
//ImageViewer1.Bitmap := MyBitmap; //does not work

//previous failure 3
//ImageViewer1.Bitmap.Assign(MyBitmap); //does not work

All of these compile, but leave a white bitmap. Only success has been:

//MyBitmap.SaveToFile( 'finmap.bmp');
//ImageViewer1.Bitmap.LoadFromFile 'finmap.bmp');

Maybe I should not argue with success, but I hate needed disk save and restore.
Eli M

Posts: 1,346
Registered: 11/9/13
Re: Firemonkey XE6 assign bitmap to tImageViewer
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 18, 2014 3:34 PM   in response to: Peter Guth in response to: Peter Guth
This is just on Windows or?

Pretty sure the default image type is PNG under Firemonkey (not BMP).

Two functions I'm using with custom a TPaintBox.

var
fdrawbmp:TBitmap;
fdrawbmprect:TRectF;
fdrawrect:TRectF;//Paint box size

fdrawrect:=RectF(0,0,self.Width, self.Height);
fdrawbmprect:=RectF(0,0,self.Width, self.Height);
fdrawbmp := TBitmap.Create(Round(fdrawbmprect.Width),Round(fdrawbmprect.Height));

procedure TMyPaintBox.SaveToBitmap(B: TBitmap; Width, Height: Integer);
begin
if B.Width = 0 then
Exit;
if fdrawbmp <> nil then
begin
B.Assign(fdrawbmp.CreateThumbnail(Width,Height));
end;
end;

procedure TMyPaintBox.LoadFromBitmap(B: TBitmap);
var
r,rd: TRectF;
begin
try
if assigned(fdrawbmp) then
begin
r:=TRectF.Create(PointF(0,0),B.Width,B.Height);
rd:=TRectF.Create(PointF(0,0),B.Width,B.Height);
fdrawbmp.Canvas.BeginScene();
fdrawbmp.Canvas.DrawBitmap(B,r,rd,1);
fdrawbmp.Canvas.EndScene;
InvalidateRect(fdrawrect);
end;
finally
end;
end;

Mike Sutton

Posts: 84
Registered: 10/2/00
Re: Firemonkey XE6 assign bitmap to tImageViewer
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 19, 2014 4:42 AM   in response to: Peter Guth in response to: Peter Guth
Peter Guth wrote:
//ImageViewer1.Bitmap.Assign(MyBitmap); //does not work

This should be the appropriate form to use.

What code are you using to create the bitmap? If complex create a simple test case which shows the failure.

Also, are you assigning an alpha channel/transparency value in your pixels? Forget that and you'll see nothing.

--
FireMonkey users guide http://bit.ly/10abRZX
Embarcadero MVP
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02