Solved

Storing bitmaps in memory

Posted on 1998-01-01
11
450 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now