How To Obtain Width and Height of a CBitmap Object (88555)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft C/C++ for MS-DOS 7.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Windows XP Home Edition
    • Microsoft Windows XP Professional

This article was previously published under Q88555
NOTE: Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model. The information in this article applies to unmanaged Visual C++ code only.

SUMMARY

The CBitmap class provided by Microsoft Foundation Classes (MFC), does not contain member functions that return the width and height of a bitmap.

MORE INFORMATION

The width and height of a CBitmap may be obtained by using the member function GetObject(). GetObject() can return a BITMAP structure that contains both the height and the width of the bitmap, along with some additional information.

The sample code below illustrates two functions that accept a CBitmap reference and return the width and height contained in the BITMAP structure returned by GetObject().

Sample Code

   int GetCBitmapWidth(const CBitmap & cbm)
   {
      BITMAP bm;
      cbm.GetObject(sizeof(BITMAP),&bm);
      return bm.bmWidth;
   }

   int GetCBitmapHeight(const CBitmap & cbm)
   {
      BITMAP bm;
      cbm.GetObject(sizeof(BITMAP),&bm);
      return bm.bmHeight;
   }
				

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbBitmap kbGDI kbhowto KB88555