OutOfLuck
asked on
Problem with loading a Bitmap image
hi i am having trouble loading a bitmap image into a DrawGrid.
i am using the following code to load the pictures:
function GetBitmap(BitmapName: string): TBitmap;
var i: Integer;
begin
Result := nil;
for I := 1 to 20 do
if SameText(ImagePaths[i], BitmapName) then
begin
Result := Images[i].Picture.Bitmap; //CRASHES ON THIS LINE
Break;
end;
end;
i call this function during the following line:
DrawGrid1.Canvas.Draw(Rect .Left,Rect .Top,GetBi tmap(xArra y[ARow, ACol, c_pic]))
xArray[ARow, ACol, c_pic] contains the picture name for example 'house.bmp' and is an array of string.
i am using the following code to load the pictures:
function GetBitmap(BitmapName: string): TBitmap;
var i: Integer;
begin
Result := nil;
for I := 1 to 20 do
if SameText(ImagePaths[i], BitmapName) then
begin
Result := Images[i].Picture.Bitmap; //CRASHES ON THIS LINE
Break;
end;
end;
i call this function during the following line:
DrawGrid1.Canvas.Draw(Rect
xArray[ARow, ACol, c_pic] contains the picture name for example 'house.bmp' and is an array of string.
should it be zero based?
e.g.
for i := 0 to 19
if "images" is a list, then it usually starts at zero
e.g.
for i := 0 to 19
if "images" is a list, then it usually starts at zero
ASKER
hi thankyou for the link, but it doesnt help me solve my problem becasue in their code they use a procedure to load images whilst i need to use a function.
also i changed me values to 0 as well and that also didnt fix the problem.
also i changed me values to 0 as well and that also didnt fix the problem.
The problem is because you have not actually loaded the images into the Images object yet
therefore when you try to access the TImage in "Images[i]" it causes an error
You need a procedure like this :-
procedure LoadImages;var i: integer;begin For i := 0 to 19 do begin if not assigned(Images[i]) then Images[i] := TImage.Create(nil); Images[i].Picture.LoadFrom File(Image Paths[i]); end;end;
And you need to place a call to it in your Form's create method
e.g.
procedure TfrmRooms.FormCreate(Sende r: TObject);
begin
LoadImages;
// other code here...
end;
You _do_ also need to change your for loop to be 0 based, as your Images is an array [0..19] of TImage
function GetBitmap(BitmapName: string): TBitmap;
var i: Integer;
begin
Result := nil;
for I := 0 to 19 do // <-- zero based, not 1-20
if SameText(ImagePaths[i], BitmapName) then
begin
Result := Images[i].Picture.Bitmap; //CRASHES ON THIS LINE
Break;
end;
end;
Loki
therefore when you try to access the TImage in "Images[i]" it causes an error
You need a procedure like this :-
procedure LoadImages;var i: integer;begin For i := 0 to 19 do begin if not assigned(Images[i]) then Images[i] := TImage.Create(nil); Images[i].Picture.LoadFrom
And you need to place a call to it in your Form's create method
e.g.
procedure TfrmRooms.FormCreate(Sende
begin
LoadImages;
// other code here...
end;
You _do_ also need to change your for loop to be 0 based, as your Images is an array [0..19] of TImage
function GetBitmap(BitmapName: string): TBitmap;
var i: Integer;
begin
Result := nil;
for I := 0 to 19 do // <-- zero based, not 1-20
if SameText(ImagePaths[i], BitmapName) then
begin
Result := Images[i].Picture.Bitmap; //CRASHES ON THIS LINE
Break;
end;
end;
Loki
ASKER
so basically you need a procedure and a function to make this work?
I was trying to work with your existing code....
Personally, I'd rewrite the whole thing ;-)
you could make it really simple by just doing this :-
function GetBitmap(BitmapName: string): TBitmap;
begin
result := nil;
if pos('.bmp', BitmapName) > 0 then
begin
result := TBitmap.Create;
result.LoadFromFile(Bitmap Name);
end;
end;
Personally, I'd rewrite the whole thing ;-)
you could make it really simple by just doing this :-
function GetBitmap(BitmapName: string): TBitmap;
begin
result := nil;
if pos('.bmp', BitmapName) > 0 then
begin
result := TBitmap.Create;
result.LoadFromFile(Bitmap
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
okay well I do have another code but the problem with that is for some reason it will not load all th pictures into the grid it repeats quite a few pictures and only let's me display one picture at a time, should I post that code up?
from your other posts it appears you only show the image on click
is that right?
is that right?
ASKER
yes that is correct
I suggest you take a look
http://www.delphi-central.com/tutorials/memory_game.aspx