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.
nir_lAsked:
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.

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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

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
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
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
Delphi

From novice to tech pro — start learning today.