Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Create Bitmap on runtime

qhimqqhimq Posts: 16Member
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

  • cactus1cactus1 Posts: 41Member
    : 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
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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]
  • qhimqqhimq Posts: 16Member
    Thanks to both of you.
  • Josh CodeJosh Code Posts: 675Member
    : 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.