• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

Storing bitmaps in memory

My question concerns storing bitmaps in memory. I wish to
store bitmapped data in memory but I get a problem with
Windows' resources. I think it has something to do with
the device context connected to every bitmap I create. I
guess those device contexts are rather limited, so my
question is how can I store a data from bitmap in some
memory segment and then return it to bitmap form. I've
seen that there's a GetDIB function in the System unit,
but I haven't found something like SetDIB... Any
suggestions ( best with code implementations ) would be
appriciated, 10q.
0
nir_l
Asked:
nir_l
  • 4
  • 3
  • 2
  • +2
1 Solution
 
MatveyCommented:
Well, what for do you need to store it this way?
Why not create a TBitmap object and load the BMP into it?

Matvey
0
 
nir_lAuthor Commented:
That's excatly what I currently do. The problem is ( as
presented ) that a TBitmap needs a device context which
is a windows resource. When you use 100s of TBitmaps your
resources run out EVEN if you have enough memory. I try
to use GetDIB to store only the bitmap data and not the
data and resources needed.
0
 
MatveyCommented:
How many colors dos your BMP have? (2, 16, 256...)
And which version of Delphi do you use? Because it depands, I had the same problem...

Matvey
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
nir_lAuthor Commented:
I use Delphi 1.0 under W95. I also use DDBs ( that's what
TBitmap is ) and not DIBs, although I would rather use
the latter...
I have to store a few hundreds of bitmaps.
0
 
MatveyCommented:
If you have enough resources you shell have no problems when the palete of your TBitmap is correct. My problem was:
When I used a 20MB- 16 colors BMP, I also ran out of resources because the palete was set to 256 colors.

So try limiting the palete (I think you can do it by loading some BMP with a defined range of colors (examples from Delphi in the images/splash/16color or smth like that, before you DRAW yours on it).
If I'll find it, I'll send you the code, not right now...

So try doing smth like that, good luck to you and a happy new year!
Matvey
0
 
bcrotazCommented:
Try creating one bitmap, and putting all your bitmaps in it, using Canvas.Draw(0,y,MyBmp), and make 'y' the current height of the stored bmp.  Then just increase the height of the bmp just before you add a bmp.
Put it together into a component, and it should be reasonably simple.  Just keep a list of the y positions and heights of each bmp.  If the widths are variable, make the stored bmp width large enough to hold the largest currently stored bmp.  Height and width can be dynamic.

Good Luck.

bryan  
0
 
nir_lAuthor Commented:
Matvey - I have enough system resources so the fact
the bitmaps are true-color doesn't realy hurt me.
However, they do waste much more space in memory so
I'd rather save them in memory in their default
color and convert them to true color only when needed.
I didn't find any sources of doing that, however.

Bcrotaz - I've got the small bitmaps suggestions tons
of times and they won't do because my bitmaps vary
in size. Using this way would be very clumsy and
resource consuming.


Thanks for the suggestions.
0
 
ZifNabCommented:
Hi nir_l,

maybe, setdib is not the correct word, what about createdib?

Look for an example?

www.delphideli.com/download/dibdemo.zip

Hope this is what you were looking for.

Have fun,
c.u. ZifNab;


0
 
rchambelCommented:
You have a few functions to create a memory handle like good old pascal's GetMem().

GlobalAlloc(): Create a memory handle.
GlobalLock(): Lock the memory (windows can move memory to fill in the memory holes left my other memory allocations) and obtain a pointer.
GlobalUnlock(): to release that pointer and enable Windows to take control of that piece of memory.
GlobalFree(): Release the memory handle.

And they're used in that order.

After you create the memory handle, DO NOT destroy or change it! that handle is a pointer to Windows Global memory and Windows itself is responsible for it's maintainance.

When you GlobalLock(theHandle) you get a pointer to that memory.
You can do CopyMemory() from the Bitmap's handle to the new handle.
After that, you may GlobalUnlock(thePointer) and whenever you need that pointer again you re-lock it - remember it's a new pointer since Windows might have changed its position.

After you've finished using that memory, GlobalFree(theHandle).

Tomorrow, I'll post you a comment using this functions.

NOW, for the easy part: Use AllocMem (it GlobalAlloc()s  and GlobalLock()s all at the same time) and FreeMem() to free it!
0
 
nir_lAuthor Commented:
The demo shows also how to show images. And it's an example of creating dib's, I guess they also show how you can put these on to screen.  
0
 
ZifNabCommented:
Sorry, haven't seen your commet before, I shall
check this address now... 10q.
0
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.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now