1

I am having a memory issue with an algorithm I am using to "Flatten" a page in a PDF document.

HBITMAP hbmp = CreateDibSection(...);
ThirdPartyBmpManipulation(hbmp, "C:\\file.pdf", 0); //renders page 0 in file.pdf
void * hdib = ConvertBitmap(hbmp); //copy a Dib Section to a Dib
DeleteObject(hbmp); //frees the HBitmap while the Dib is now in memory

The problem is I have a really large bitmap and in some cases I cannot keep the HBitmap in memory while I allocate the DIB to be copied to.

So it is a long shot, but can I somehow allocate the Dib Section on disk and still have an HBITMAP for it? (use the same handle for my ConvertBitmap function)

Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
  • Sounds like a good question. I would be interested in knowing the answer. But why do you still use the old Windows API?!! That's REALLY old. Why not use GDI+? – Rafid Jan 13 '11 at 17:13
  • @Promather GDI+ is just a wrapper around GDI that makes it really slow ;-) – David Heffernan Jan 13 '11 at 17:27
  • Hmmm.... I don't I agree with you David. GDI+ is supposed to be a completely new things that with Windows Vista and 7 started to use graphics acceleration. Am I correct, or mixing up things? Anyway, even if it is slower, it may have a better structure that makes it easy to read/write bitmaps from the hard drive. – Rafid Jan 13 '11 at 17:43
  • Any chance of using the DIB section as-is without having to copy it? – Mark Ransom Jan 13 '11 at 18:25
  • GDI+ came out before vista, so I'm not sure thats right. You can use any device that supports raster operations, which is why I think using a HDD wont work. Its worth a shot. – Tom Fobear Jan 13 '11 at 18:26
  • @Promather It came out with XP and can be installed on Win2k and even Win 9x quite easily. I think you are confusing GDI+ with DWM or some other Vista-ism. – David Heffernan Jan 13 '11 at 18:27
  • @mark no, unfortunately All other image processing I do on the image takes a DIB, so I must convert the dib section. perhaps i can create a DIB with GDI? – Tom Fobear Jan 13 '11 at 18:28

1 Answers1

1

What is the failure mode? You say "physical memory": windows uses virtual memory, it will page to disk, you don't need to worry about running out of physical memory beyond the performance hit. If you're running out of address space, using the disk may not be able to help you.

However, CreateDIBSection can take a HANDLE to a file mapping (created with CreateFileMapping ). If the internals of the function and related HBITMAP functions are smart enough, it may be possible to avoid exhausting your address space by leveraging that capability. If they are "smart" they will use MapViewOfFile to map relatively small "windows" of the file as needed into your process's address space.

Logan Capaldo
  • 39,555
  • 5
  • 63
  • 78
  • I meant adress space, i think. I thought CreateFileMapping caused the program to reserve as much space in memory as the file its reading from? Are you saying that CreateFileMapping will not count towards win32's 2Gb limit? – Tom Fobear Jan 13 '11 at 18:37
  • CreateFileMapping by itself doesn't do anything to your process's address space, MapViewOfFile[Ex] does. It is possible to map portions of a file into your address space at a time. If `CreateDIBSection` and friends are smart enough to do this when given a HANDLE to a mapping, you may be able to avoid this problem. Again, no guarantees, I don't know the internals of these functions, I only know that given the interfaces it's possible they could work like this. – Logan Capaldo Jan 13 '11 at 18:59
  • It worked, however the copy still did not work on my boss's computer. I believe some of it has to do with the application hes running the algorithm in may be cutting into the process's mem. Mapping the file reduced it a bit, but I may have to reduce the resulting bitmaps size yet.. Currently it is rendering the bitmap at 300 dpi, perhaps I can lower this... – Tom Fobear Jan 13 '11 at 20:33