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?
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 AnifantakisSoftware EngineerCommented:
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
0
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
0
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

0
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

0

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?
0
TheRealLokiSenior DeveloperCommented:
from your other posts it appears you only show the image on click
is that right?
0
OutOfLuckAuthor Commented:
yes that is correct
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.