c++cwinapidib

How cheap/expensive is memory DC in Win API?


I am working on a program that will have many DIB bitmaps (created by CreateDIBSection) and will have to draw a lot of text on them using Win API.

In order to draw on the bitmap, Windows needs device context, created by CreateCompatibleDC.

And now here are two approaches:

  1. I can create the DC once per bitmap, using it for drawing and delete it when freeing the bitmap.

  2. Or I can create DC only when I need to draw to the bitmap, call the draw functions and delete the DC.

What is the better approach? I prefer the first, because of less calls - this will make my code much smaller and also a little bit faster.

But isn't it too expensive to hold a long living DC for every bitmap?

Edit1: The application is actually a GUI toolkit library that can be used in different and unpredictable way in the future, so I need a well balanced decision with maximal possible performance and minimal system resource usage.


Solution

  • GDI objects are limited, both per process as well as per session. You are competing for resources with all other processes running in the same session. With that in mind, you should consume GDI resources only when needed (option 2 in your question).

    Mark Russinovich's blog entry Pushing the Limits of Windows: USER and GDI Objects – Part 2 goes into a fair amount of detail. To sum up the gist, here is a list of limits that the window manager places on GDI resources: