r/C_Programming • u/Boggy_Old_Fart • Jul 28 '26
Draw on Bitmap to file, NOT to screen
I want to create high-resolution bitmap files for print publication, much higher resolution than the screen. I can do it with direct bit/byte manipulation, but I'd much rather use GDI with brushes and LineTo. But while I can create compatible bitmaps and draw on them, I haven't found a way to get from the drawing DC back to real memory (which I can write to a file), only to the screen. Windows seems to block every approach I've tried. (Like, I can BitBlt to the screen, but not to the handle of the original bitmap... gotta be a DC, and Windows won't allow a DC to the bitmap... arghh!) Anyone know how to do this?
Many, many thanks!
24
u/PseudoFrequency Jul 28 '26
This is more of a Win32 question than C. Is GetDIBits what you want?Â
2
u/timrprobocom Jul 29 '26
CreateDIBSectionis the right answer. Remember thatGetDIBitscreates a COPY, because a compatible bitmap is in the same format as the desktop, which is not under your control. Might be wr but, might be 32 but.Also, make sure your expectations are correct. Most commercial print shops run 150 or 300 DPI, which is not that much higher than your screen.
7
u/kabekew Jul 29 '26
If GetDIBits on your bitmap's DC is failing, it may be because it's in graphic card memory only. Try creating a memory DC and memory Bitmap with CreateCompatibleDC and CreateCompatibleBitmap (off your bitmap's DC and hBitmap respectively), SelectObject the memory bitmap into the memory DC, bitblt your bitmapDC into that memoryDC, then try GetDIBits on the memoryDC/Bitmap.
4
u/HalifaxRoad Jul 28 '26
I have done lots of bitmap rendering, but it was in c#
It was super annoying not gonna lie. I remember I had to use marshal to move everything into unmanaged memory, and then pull it back out when I was done.
4
u/fdwr Jul 29 '26 edited Jul 29 '26
Just call
CreateDIBSection(example copied below), which returns the memory pointer to the first pixel and anHBITMAP(that you can select into yourHDC).2
u/kun1z Jul 28 '26
https://learn.microsoft.com/en-us/windows/win32/gdi/storing-an-image
https://learn.microsoft.com/en-us/windows/win32/gdi/bitmap-functions
1
u/MyNameIsHaines Jul 29 '26
It's fairly easy to make your own SaveToFile function that takes the pixel data from your compatible DC.
1
u/catbrane Jul 29 '26
Could you use SVG? Make your drawing in eg. inkscape, then save as a high-res bitmap. You can go up to 30k x 30k pixels with inkscape's "export as". I expect you can go higher with other SVG renderers.
0
u/chrism239 Jul 29 '26
Very easy in wxWidgets (with C++ but not C).