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.

OutOfLuckAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis AnifantakisProgramming InstructorCommented:
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
TheRealLokiSenior DeveloperCommented:
should it be zero based?
e.g.
for i := 0 to 19

if "images" is a list, then it usually starts at zero
OutOfLuckAuthor Commented:
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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

TheRealLokiSenior DeveloperCommented:
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
OutOfLuckAuthor Commented:
so basically you need a procedure and a function to make this work?
TheRealLokiSenior DeveloperCommented:
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;

TheRealLokiSenior DeveloperCommented:
actually, a better more dynamic solution would be to load teh images unless they are already loaded
therefore, if an image is never used, it is never loaded.
once it is used, it is then available for all other tiles, and does not need to be reloaded


function GetBitmap(BitmapName: string): TBitmap;
begin
 Result := nil;
 for I := 0 to 19 do
   if SameText(ImagePaths[i], BitmapName) then
   begin
     if not assigned(Images[i]) then
     begin
       Images[i] := TImage.Create(nil);
       Images[i].Picture.LoadFromFile(BitmapName);
     end;
     Result := Images[i].Picture.Bitmap;
     Break;
   end;
end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OutOfLuckAuthor Commented:
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?
TheRealLokiSenior DeveloperCommented:
from your other posts it appears you only show the image on click
is that right?
OutOfLuckAuthor Commented:
yes that is correct
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.