cando
asked on
Array of Bitmaps
I have a form with a listbox on the left and an image control on the right.
When you click something in the listbox, it needs to display the image to go with it. The only way I could think to do this was to declare an array of tbitmap and when you click the listbox, the array with the listbox's itemindex shows in the image control. When I try to clear the whole thing, the array, the image and the listbox, it appears to clear. When you reload every thing new, the listitems that contained images before, still do. I have been having alot of access errors trying to free the whole array eachtime.
The array needs to hold at least 75 images. Any Ideas?
Thanks in advance,
Cando
When you click something in the listbox, it needs to display the image to go with it. The only way I could think to do this was to declare an array of tbitmap and when you click the listbox, the array with the listbox's itemindex shows in the image control. When I try to clear the whole thing, the array, the image and the listbox, it appears to clear. When you reload every thing new, the listitems that contained images before, still do. I have been having alot of access errors trying to free the whole array eachtime.
The array needs to hold at least 75 images. Any Ideas?
Thanks in advance,
Cando
Maintain a TObjectList next to the ListBox and store the bitmaps in this list. TObjectList frees the bitmaps automatically when necessary...
TImageList can only hold images of equal size. Maybe this is case here... don't know.
ASKER
Sorry,
All the images could be very different, loaded from files or pasted form the clipboard.
All the images could be very different, loaded from files or pasted form the clipboard.
Can you post the relevant code?
Cheers,
Raymond.
Cheers,
Raymond.
ASKER
FImage: array[1..75] of TBitmap;
procedure TForm1.LoadFromFile1Click( Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
FImage[ListBox2.ItemIndex + 1] := TBitmap.Create;
FImage[ListBox2.ItemIndex + 1].LoadFromFile(openPictur eDialog1.F IleName);
Image1.Picture.Bitmap := FImage[ListBox2.ItemIndex + 1];
end;
end;
procedure TForm1.ListBox2Click(Sende r: TObject);
begin
try Image1.Picture.Bitmap := FImage[ListBox2.ItemIndex + 1] except end;
end;
This all seems to work alright When I try to clear it all so it can be refilled, it clears but when the listbox is filled again, it has pictures for the items that had them last time. I dont know how to clear the array.
Here is the code I use to clear everything.
procedure TForm1.ClearAll;
var
i: integer;
begin
Image1.Picture := nil;
ListBox2.Clear;
try
for I := 1 to 75 do FImage[i].free;
except end;
end;
procedure TForm1.LoadFromFile1Click(
begin
if OpenPictureDialog1.Execute
begin
FImage[ListBox2.ItemIndex + 1] := TBitmap.Create;
FImage[ListBox2.ItemIndex + 1].LoadFromFile(openPictur
Image1.Picture.Bitmap := FImage[ListBox2.ItemIndex + 1];
end;
end;
procedure TForm1.ListBox2Click(Sende
begin
try Image1.Picture.Bitmap := FImage[ListBox2.ItemIndex + 1] except end;
end;
This all seems to work alright When I try to clear it all so it can be refilled, it clears but when the listbox is filled again, it has pictures for the items that had them last time. I dont know how to clear the array.
Here is the code I use to clear everything.
procedure TForm1.ClearAll;
var
i: integer;
begin
Image1.Picture := nil;
ListBox2.Clear;
try
for I := 1 to 75 do FImage[i].free;
except end;
end;
When you add the items to the listbox, use addobject
a sample (form with button1, listbox1, image1 and openpicturedialog1)
procedure TForm1.Button1Click(Sender : TObject);
var
mybitmap : tbitmap;
begin
if openpicturedialog1.execute then
begin
mybitmap := tbitmap.create;
mybitmap.loadfromfile(open picturedia log1.filen ame);
listbox1.items.addobject ('item', TObject(mybitmap));
end;
When you delete an item from the list box, don't forget to call TBitmap(listbox1.items.obj ect[x]).fr ee first.
Mark
end;
procedure TForm1.ListBox1Click(Sende r: TObject);
begin
image1.picture.bitmap.assi gn(TBitmap (listbox1. items.obje cts[listbo x1.itemind ex]));
end;
a sample (form with button1, listbox1, image1 and openpicturedialog1)
procedure TForm1.Button1Click(Sender
var
mybitmap : tbitmap;
begin
if openpicturedialog1.execute
begin
mybitmap := tbitmap.create;
mybitmap.loadfromfile(open
listbox1.items.addobject ('item', TObject(mybitmap));
end;
When you delete an item from the list box, don't forget to call TBitmap(listbox1.items.obj
Mark
end;
procedure TForm1.ListBox1Click(Sende
begin
image1.picture.bitmap.assi
end;
Actually, if you are working in Delphi 4 or higher, you could use a dynamic array of Bitmaps, incrementing it as you poplulate it:
Var BMPArray:array of Tbitmap;
Begin
BmpArray[length(bmpArray)- 1]:=Tbitma p.create;
BmpArray[length(bmpArray)- 1].LoadFro mfile('yad a.bmp');
end;
and then scaling that array down to
nothing again by using Copy:
bmpArray:=Copy(BmpArray,0, 0);
Var BMPArray:array of Tbitmap;
Begin
BmpArray[length(bmpArray)-
BmpArray[length(bmpArray)-
end;
and then scaling that array down to
nothing again by using Copy:
bmpArray:=Copy(BmpArray,0,
Oops, my last comment about delete is in the middle of my sample, sorry 'bout that... below is a better example, button2 is a delete item button (no error checking..)
procedure TForm1.Button1Click(Sender : TObject);
var
mybitmap : tbitmap;
begin
if openpicturedialog1.execute then
begin
mybitmap := tbitmap.create;
mybitmap.loadfromfile(open picturedia log1.filen ame);
listbox1.items.addobject ('item', TObject(mybitmap));
end;
end;
procedure TForm1.ListBox1Click(Sende r: TObject);
begin
if assigned(listbox1.items.ob jects[list box1.itemi ndex]) then
image1.picture.bitmap.assi gn(TBitmap (listbox1. items.obje cts[listbo x1.itemind ex]));
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
TBitmap(listbox1.items.obj ects[listb ox1.itemin dex]).free ;
TBitmap(listbox1.items.obj ects[listb ox1.itemin dex]).Assi gn(nil);
end;
procedure TForm1.Button1Click(Sender
var
mybitmap : tbitmap;
begin
if openpicturedialog1.execute
begin
mybitmap := tbitmap.create;
mybitmap.loadfromfile(open
listbox1.items.addobject ('item', TObject(mybitmap));
end;
end;
procedure TForm1.ListBox1Click(Sende
begin
if assigned(listbox1.items.ob
image1.picture.bitmap.assi
end;
procedure TForm1.Button2Click(Sender
begin
TBitmap(listbox1.items.obj
TBitmap(listbox1.items.obj
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(Sorry if I've misinterpreted things and stated the bleedin' obvious)
Gotta dash,
Steven