Solved

Storing bitmaps in memory

Posted on 1998-01-01
11
456 Views
Last Modified: 2010-04-04
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
Comment
Question by:nir_l
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1355054
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
 

Author Comment

by:nir_l
ID: 1355055
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355056
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nir_l
ID: 1355057
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
 
LVL 3

Expert Comment

by:Matvey
ID: 1355058
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
 
LVL 1

Expert Comment

by:bcrotaz
ID: 1355059
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
 

Author Comment

by:nir_l
ID: 1355060
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1355061
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
 
LVL 1

Accepted Solution

by:
rchambel earned 200 total points
ID: 1355062
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
 

Author Comment

by:nir_l
ID: 1355063
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
 
LVL 8

Expert Comment

by:ZifNab
ID: 1355064
Sorry, haven't seen your commet before, I shall
check this address now... 10q.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question