CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2007
    Location
    South Africa
    Posts
    86

    CreateCompatibleBitmap

    Is there a way to obtain the width and height (in pixels) of a DC?

    If you take the following code for example:

    Code:
    HDC memDC = CreateCompatibleDC ( hDC );
    HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
    SelectObject ( memDC, memBM );
    How can I obtain the width and height of hDC so that I can pass the proper values for nWidth and nHeight so that the created bitmap is exactly the same size as the hDC surface?

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: CreateCompatibleBitmap

    The DC does not have a width or height. It is just an interface for drawing onto a surface.

    You have to use some other method to get the dimensions of the window etc. that the DC draws onto.
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Apr 2007
    Location
    South Africa
    Posts
    86

    Re: CreateCompatibleBitmap

    Isn't there a bitmap that is associated with a DC? If so, is there a way for me to access it so that I can get the width and height of the bitmap?

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: CreateCompatibleBitmap

    You might be able to use GetCurrentObject to get the currently selected bitmap.

    Viggy

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: CreateCompatibleBitmap

    Quote Originally Posted by links View Post
    Isn't there a bitmap that is associated with a DC? If so, is there a way for me to access it so that I can get the width and height of the bitmap?
    Not necessarily. A DC with a bitmap is called a memory DC.
    Nobody cares how it works as long as it works

  6. #6
    Join Date
    Apr 2007
    Location
    South Africa
    Posts
    86

    Re: CreateCompatibleBitmap

    Thanks for all the replies. You guys are really helpful.

  7. #7
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: CreateCompatibleBitmap

    Quote Originally Posted by zerver View Post
    A DC with a bitmap is called a memory DC.

    I have to admit, this is confusing. Could you elaborate?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: CreateCompatibleBitmap

    Quote Originally Posted by JohnCz View Post

    I have to admit, this is confusing. Could you elaborate?
    Well, if I'm not completely mistaken a DC normally draws directly to screen. For instance, GetDC(NULL) will obtain a DC that draws directly on top of the entire screen surface. Such a DC does not have an associated bitmap, and GetCurrentObject would not return anything useful.

    If you select a bitmap into the DC, it will draw onto this bitmap instead. Common uses for this are double-buffering (to reduce flicker), but also to generate an image that can be saved to disk. The reason it is called memory DC is that it draws into computer memory rather than a graphics buffer. However, this is not entirely true since device dependent bitmaps very well may be uploaded to the graphics memory for video acceleration purposes. This is done transparently by the system and graphics drivers.
    Nobody cares how it works as long as it works

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: CreateCompatibleBitmap

    Quote Originally Posted by zerver View Post
    A DC with a bitmap is called a memory DC.
    Well you stated that DC with bitmap is called memory DC. Not necessarily. You can select bitmap to a window’s device context directly and use it to transfer image.
    CreteCompatibleDC, creates device context compatible with window’s device context, in memory; this constitutes so called memory DC or buffer DC. Memory DC is not a specific name that is used for a categorization - it is just most popular.
    Created buffer can be used to reduce flickering by transferring image to a buffer, do some drawing and transfer all to a Windows DC. It is also used to store temporally window’s device context to display something else and return to original context.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: CreateCompatibleBitmap

    Quote Originally Posted by JohnCz View Post
    You can select bitmap to a window’s device context directly and use it to transfer image.
    Are you sure?
    According to MSDN:
    Bitmaps can be selected for memory device contexts only, and for only one device context at a time.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #11
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: CreateCompatibleBitmap

    Quote Originally Posted by VladimirF View Post
    Are you sure?
    Well, not anymore. That happens when posting without testing. I could have sworn, that I had an application where I selected a bitmap directly to a windows dc, however after finding it, I realized I used BitBlt.
    Thanks for correcting and sorry for a confusion.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  12. #12
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: CreateCompatibleBitmap

    Quote Originally Posted by VladimirF View Post
    Are you sure?
    According to MSDN:
    Bitmaps can be selected for memory device contexts only, and for only one device context at a time.
    This is correct, although it is possible to work around by creating two device independent bitmaps that share the same pixel memory.
    Nobody cares how it works as long as it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured