Link to home
Start Free TrialLog in
Avatar of OutOfLuck
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,GetBitmap(xArray[ARow, ACol, c_pic]))

xArray[ARow, ACol, c_pic] contains the picture name for example 'house.bmp' and is an array of string.

Avatar of Ioannis Anifantakis
Ioannis Anifantakis
Flag of Greece image

Here is a very explanatory tutorial about use of DrawGrid.
I suggest you take a look

http://www.delphi-central.com/tutorials/memory_game.aspx
Avatar of TheRealLoki
should it be zero based?
e.g.
for i := 0 to 19

if "images" is a list, then it usually starts at zero
Avatar of OutOfLuck
OutOfLuck

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.
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.LoadFromFile(ImagePaths[i]);  end;end;

And you need to place a call to it in your Form's create method
e.g.

procedure TfrmRooms.FormCreate(Sender: 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
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(BitmapName);
  end;
end;

ASKER CERTIFIED SOLUTION
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
yes that is correct