Create Bitmap on runtime

Hi,

I'm attempting to use c++, windows api GDI, to create a bitmap of something I painted on the screen. Once I create the bitmap I want to be able to move it about the window.

I couldn't find anything too great on google about it, so I broke down and I'm asking for your help.

If you don't know, possibly if you point me in the direction of a tutorial/code of how to make mspaint, I'd be able to figure it out.

Thanks.

Comments

  • : Hi,
    :
    : I'm attempting to use c++, windows api GDI, to create a bitmap of
    : something I painted on the screen. Once I create the bitmap I want
    : to be able to move it about the window.
    :
    : I couldn't find anything too great on google about it, so I broke
    : down and I'm asking for your help.
    :
    : If you don't know, possibly if you point me in the direction of a
    : tutorial/code of how to make mspaint, I'd be able to figure it out.
    :
    : Thanks.
    :
    If you run Windows, the Windows API provides you with several bitmap operations which suit your needs.
    http://msdn2.microsoft.com/en-us/library/ms532343.aspx
  • [color=Blue]
    1. Get a device context of the area you trying to snap shoot.
    - use GetDC()

    2. Create a DC compatible with DC from step 1.
    - use CreateCompatibleDC()

    3. Create a bitmap handle compatible with DC from step 1.
    You will need to know the area size (width and height)
    - use CreateCompatibleBitmap()

    4. Select HBITMAP from step 3 into DC from step 2. You need
    to preserve the return value (it is HBITMAP) of this operation.
    - use SelectObject()

    5. Make a snap shot of the area.
    - use BitBlt()
    - the source DC is from step 1
    - the destination DC is from step 2
    - the coordinates and size will define the area itself

    6. Restore the HBITMAP in a DC from step 2 - use the returned value
    in a call to SelectObject()

    7. Get rid of DC from step 2
    - use DeleteDC()

    ===

    That's it - at this point you left with HBITMAP created in step 3 and it has the image of the are you need, so you can draw it somewhere else or save it as a file. To save it - you need to do more stuff...

    When you finished with HBITMAP - do not forget to dismiss it using DeleteObject() function.
    [/color]
  • Thanks to both of you.
  • : Thanks to both of you.
    :
    I made a c++ class for working with the bitmap format. It works independent of the Windows API, though.

    You can get the c++ source code here:
    http://www.programmersheaven.com/d/click.aspx?ID=F54666
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories