0

Trying to set up an Opengl context to render into a bitmap but I've found the handle returned from the Tbimap canvas object keeps changing so the rendering context keeps throwing an exception because it doesn't match the handle the rendering context was created with.

I found changing the bitmaps width or height will change the handle value, and if the bitmap is created outside of the procedure that does the rendering, you get a different handle each time you enter the procedure.

I need this to stay static or its all a bit pointless having the ability to render to a bitmap in the first place.

Any clues how to fix the handle?

Andy k
  • 1,056
  • 1
  • 11
  • 22

1 Answers1

6

Windows bitmap objects have fixed width and height. When you create an HBITMAP you have to decide once and for all on the height, width and indeed other properties such as pixel format. So, of course when you modify the Width and Height property of a Delphi TBitmap, then the implementing HBITMAP will need to be re-created.

It seems that you are asking to be able to create an HBITMAP that has width and height properties that can be modified. No such thing exists.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That's a bummer, so I can only render into the bitmap once and thats it ? There is no such limitation with rendering to the screen, I can resize the screen at will without penalty. Thing is, the rendering includes text and building fonts is very slow in openGL, so recreating the rendering context every time is out of the question. – Andy k Mar 11 '13 at 10:19
  • 4
    @Andyk: You can render into it as many times as you like, only you don't change the dimensions of the bitmap. But surely, that's nothing you do very often, is it? – Andreas Rejbrand Mar 11 '13 at 10:20
  • @Andreas. The output is for rendering for printer output but the print preview screen for said output can be resized by the user. – Andy k Mar 11 '13 at 10:26
  • Can you create a larger bitmap than you need and only render to the top left portion of it? – David Heffernan Mar 11 '13 at 10:27
  • @David Heffernan. That is a possibility but would need some rewrite of the renderer as it's designed to grab and use the whole screen of whatever is passed to it. That's why rendering to a resizable bitmap is so conveniant as you could set the dimensions to whatever the ultimate destination size is going to be. It might be easier to grab the bitmap and stretch it on any resize events rather than trying to re-render each time. – Andy k Mar 11 '13 at 11:07
  • 2
    Andy, you can reuse the same bitmap, but why are you modifying its size *during* a paint cycle? Draw to it for the print preview, and then resize it and draw to it again for the real print. Why does the renderer care whether it's the same bitmap? The second render should be independent of the first, so the original handle shouldn't matter. The renderer should be leaving the device context in a consistent state between calls. If it's not, you'll need to fix that. – Rob Kennedy Mar 11 '13 at 13:06
  • 1
    @RobKennedy I think the difficulty is OpenGL. I'm not familiar with OpenGL but I have some experience with Direct3D. For D3D if you change the size of the output window then you have to re-create a context that takes serious time to initialize. Like a couple of seconds. If this was GDI then there would be no problem. – David Heffernan Mar 11 '13 at 13:37