Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jonne

Array of tbitmap

This topic is 5248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

var pictures: array[1..10] of TBitmap; . . . //buttwhenclickedon pictures[1].LoadFromFile(''picture.bmp''); Now program crashes with message "access violation..." What is wrong in this code?

Share this post


Link to post
Share on other sites
Advertisement
You have to create classes before you use them (unless they've been dumped onto a form, in which case they'll be created and destroyed automatically) and destroy them afterwards.

So:

var
pictures: array[1..10] of TBitmap;
i: Integer;
begin
for i := Low(Pictures) to High(Pictures) do
begin
Pictures[ i] := TBitmap.Create; // important!
Pictures[ i].LoadFromFile(somewhere);
end;

// do other stuff

//...

for i := Low(Pictures) to High(Pictures) do
Pictures[ i].Free; // important!
end;

You always want to use .Create and .Free before and after using classes. Incidentally, visit here.

[edited by - Alimonster on January 12, 2004 9:04:29 AM]

Share this post


Link to post
Share on other sites
Then, how to draw picture?

I''ve tried this:

pictures[1].canvas.textout(100, 100, ''jj'');

but nothing happens.

Share this post


Link to post
Share on other sites
The bitmaps themselves are being modified, but you have to display the bitmaps at some point to see the changes. The bitmaps themselves are just blobs of memory somewhere inside the computer, and won't be visible until you say, in effect, "display this blob of memory somewhere." The simplest way to display the bitmap(s) is to use Canvas.Draw(x, y, Pictures[1]);

You have to keep in mind that changes displayed to the screen only exist until the next time the window needs repainted (e.g. when the window is dragged off screen and back on again). If you're having problems then either make sure you're handling the OnPaint method of whatever it is you're displaying the bitmaps in, or use something where the changes persist (maybe a TImage or something).

But yeah, your problem can be solved just by drawing the bitmap onto the canvas of something that's visible (the form's canvas or a TPaintbox, for example).



[edited by - Alimonster on January 12, 2004 9:42:55 AM]

Share this post


Link to post
Share on other sites
Thanks for reply, now its working fine.

But more thigns what i'm wondering.

Is it possible to do something like this?

var

pictures: array[1..10] of TBitmap;
pictures_2: array[1..10] of TBitmap;
.
.
.
pictures[1] := TBitmap.Create;
pictures_2[1] := TBitmap.Create;

pictures[1].LoadFromFile('picture.bmp');

//and now the problem:

pictures_2[1].picture:=pictures[1].picture;

That last line ain't working, I know, but if you know, what i'm looking for?

To get that pictures[1] picture to pictures_2[1].

By the way, why is that Pictures[ i].Free; important?









[edited by - Jonne on January 12, 2004 10:01:14 AM]

[edited by - Jonne on January 12, 2004 10:01:54 AM]

Share this post


Link to post
Share on other sites
You''re probably looking for the "assign" method:

pictures2[blah].Assign(pictures1[blah]);

All classes in Delphi just point to memory. This means that they are four bytes in size, no matter what the actual class holds (the variables of type TBitmap, for example, would be 4 bytes long, because they only store a memory address saying "the rest of the class is over here!"). This is handled automatically by the compiler, so you don''t need to think about it too much.

So... you have 4 bytes worth of information (try ShowMessage(IntToStr(SizeOf(TBitmap))); some time). It''s hard to see how that is enough to store, say, 640 * 480 * 4 bytes of information, isn''t it. This is the reason you need to create classes - you say "I want to allocate some extra memory" and the variable points to it. However, since you''ve allocated memory, you have to free it later on.

Each program gets a fixed amount of memory (so called "stack" and "heap"). Every time you dynamically allocate memory, it comes from the heap. Eventually, the memory available will get less and less. This may not be a problem -- but if your program needs to run for a while, you''ll have to give back some of the memory you took previously (which is what .Free does here). If you''re not careful, you''ll reach a point where you request memory in an allocation (creating a class, using a dynamic "array of", or GetMem/New) and Windows will say "no, you greedy pig!" At that point, you''d be screwed.

Now, technically, Windows should be clever enough to reclaim memory allocated even if you don''t release it yourself, but it''s not good practice, and it''s likely that this will only happen once the user closes the program (i.e., at a time when it wouldn''t matter).

Because classes are a fixed size (4 bytes) and only point to a place in memory, which has to be created by you (with .Create), you have to be wary about copying from one to another. For example, the following wouldn''t work:

var
A, B: TBitmap;
begin
A := TBitmap.Create;
A.LoadFromFile(whatever);

B := A; // uh oh!

// do stuff with B
A.Free;
B.Free;
end;

The line "B := A" would make B point to the same allocated memory as A (what it got from the .Create), rather than copying its details. This is why most classes have a .Assign method that copies the details across as appropriate.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!