Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I dereference a TBitmap object ?

Status
Not open for further replies.

David28

Programmer
Nov 14, 2004
8
DE

I write a class named TPictureSet which administrates a list of TBitmap-objects.(Fitem)..
But at adding a TBitmap-Object to the list this problem occur :
I can’t store the TBitmap-Object‘s data into the new list item.


function TPictureSet.add(Picture : TBitmap):boolean;

begin

New(PItem); //reserve memory for a new list entry.
// PItem is a pointer on a TBitmap-object.
// type PBitmap = ^TBitmap end;
// PItem : PBitmap ;


PItem^ := Picture; // Here is the problem !!!!!
// Picture seems to be just a pointer !
// so just a pointer is added to the list. and
//all the other data like Bitmap,Palette,Width.and so on.....are lost.

Errormsg.Add('Pictures size is:' +inttostr(SizeOf(Picture)));
Errormsg.Add('PItem s Size is: '+inttostr(SizeOf(PItem^)));
//Errormsg is a StringList which I use for debugging.
//Both sizes (Picture and Pitem^) are 4 bytes.
//So I came to the conclusion that Picture is a pointer.

if FItems.Add(PItem) <> -1 then
//Fitems is a TList-Object
begin
Frames := Frames +1;
Seek := Seek+1;
result := true;
end
else
begin
FErrormsg.add(ImageListName+': '+'Cant append item on ImageList');
result := false;
end;
end;



How can I solve this problem ?
Thank you all for your answers.
 
TBitMap is a pointer. You don't need to have a pointer to it.

Variables of VCL type Txxxxxx are almost always pointers to an object. This is the way the Delphi Object Model is designed.




Andrew
Hampshire, UK
 
Hi David,

You're correct saying that your TPicture object a pointer is, in fact all Delphi objects are referenced internally through pointers. you can always typecast a pointer back to its original object. like this :

Code:
procedure TForm1.Button1Click(Sender: TObject);

var strlist : tstringlist;
    p   : pointer;

begin
 strlist:=TStringList.Create; //create object
 strlist.Add('1');
 p:=strlist; //p cointains now the address of strlist
 ShowMessage(TStringList(p).Text);
 FreeAndNil(strlist); //free object
end;

why don't you use a TObjectList to hold your pictures?

Code:
use ...,contnrs,...

...
var PictureSet : TObjectList;

procedure TForm1.StoreBitmap(bmp : TBitmap);
begin
 PictureSet.Add(bmp);
end;

function TForm1.ReadBitmap(Index : integer) : TBitmap;
begin
 Result:=TBitmap(PictureSet(Index));
end;


procedure TForm1.DeleteBitmap(Index : integer);
begin
 // this will delete the object reference from the list 
 // and will also free the object (our bitmap in this case)
 PictureSet.Delete(Index);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
// PictureSet must be created at some point
// using the formcreate event is perfect for this
// we pass the value true which refers to the OwnsObjects property
// this to make sure that the objects that are hold by the list are freed upon list destruction or at deletion from list
 PictureSet:=TObjectList.Create(True);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 // destroy PictureSet at form destruction
 if Assigned(PictureSet) then FreeAndNil(PictureSet);
end;

hope this is a bit clear to you...
if not, ask again :)

cheers!

--------------------------------------
What You See Is What You Get
 
Thank you all very much.
I have only Delphi 4 so the TObjectList Object was unknown to me.
I will try to get a more modern Delphi version.
 
Hi,

you can use a Tlist object then, but this involves a little more work...

--------------------------------------
What You See Is What You Get
 

Without really knowing what it is that you need to do, it's difficult to give you proper advice. Perhaps if you described a little bit more of the project we can be better equiped to help you.

In the meantime, with D4, you should take a good look at the TStringList object. It provides a place to store object pointers along with the power of searching the string list for what you want.

The only gotcha is that any objects created and added to the TStringList are not destroyed when the TStringList is destroyed. Although, depending on your actual situation, that may be a good thing.

But what are you doing with the bit maps? Can they stay on disk as .bmp files and only retrieve as needed? Do they need to be placed in a .res file? If they are resident on disk, you may not need to track pointers to bit maps, rather you may just need to track file names? Again, without knowing what you really need, it is impossible to say.


 
All this seems over complex in most cases, if the images are large do as Zathras suggests and store them in resource file.
Small bitmaps could go in a TImagelist, you can then take advantage of the builtin handleing routines, TImage.getbitmap etc.
If the number of images is fixed then you could have a simple array of images, or indeed an array of objects, with an image array in each object.
If it can be avoided (in most cases) I would recomend leaveing pointers to 'C' programmers!





Steve
Be excellent to each other and Party on!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top