Question : Returning bitmap DPI in MFC

I have the following code:

 CString szFilename ("C:\\test.BMP");
  HBITMAP hBmp = (HBITMAP)::LoadImage(
                  NULL,
                  szFilename,
                  IMAGE_BITMAP,
                  0,
                  0,
                  LR_LOADFROMFILE|LR_CREATEDIBSECTION
                  );

      m_Image.SetBitmap(hBmp);
 
 
  CBitmap bmp;
  bmp.Attach(hBmp);

  BITMAP  bi;
  bmp.GetBitmap(&bi);
  CString tmp;
 
  tmp.Format("%d", bi.bmHeight);
  m_Height.SetWindowText( tmp );
 
  tmp.Format("%d", bi.bmWidth);
  m_Width.SetWindowText( tmp );
 
 
  tmp.Format("%d", bi.bmBitsPixel);
  MessageBox(tmp);

I need to find or calculate the DPI for this image.  Any ideas?

Answer : Returning bitmap DPI in MFC

BITMAPINFOHEADER structure contains biXPelsPerMeter and biYPelsPerMeter members, which contain x and y resolution in pixels-per-meter. Possibly this is what you need, you can convert these values to pixels (dots) per inch.
To get BITMAPINFOHEADER structure from bmp file, read the BITMAPFILEHEADER structure from the beginning of the file using ReadFile function. Immidiately after this structure there is BITMAPINFO structure, and BITMAPINFOHEADER is it's first member.
So, you need to read sizeof(BITMAPINFOHEADER) bytes from bmp file starting from byte number sizeof(BITMAPFILEHEADER), to BITMAPINFOHEADER structure and get image resolution.

Random Solutions  
 
programming4us programming4us