Showing posts with label Image. Show all posts

How to get size of CBitmap ?

After you Load an bitmap image into CBitmap, you can use this code to get its size.

      CBitmap bmp;
      bmp.LoadBitmap( /*Load bitmap*/ );
      BITMAP BmpInfo;
      bmp.GetBitmap(&BmpInfo);

      int iWidth = BmpInfo.bmWidth;
int iHeight = BmpInfo.bmHeight;

How to Load and Show an image on MFC ?

Load and Show an Image on MFC. In this way, you can load an *.bmp Image to show on a component on you MFC form

// Load an Bitmap Image


HBITMAP  ImageShower::LoadBitmapFromFile(CString path)
{
      HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,
                              path,
                              IMAGE_BITMAP,0,0,
                              LR_LOADFROMFILE|LR_CREATEDIBSECTION);
      return hBmp;
}
// Show an HBITMAP on an DC


void ImageShower::ShowBitmapOnDC(HBITMAP hBmp, CWnd* showPlace)
{
      CBitmap bmp;
      bmp.Attach(hBmp);

      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);
     
      // Get rectangle of show place
      CRect rec;
      showPlace->GetWindowRect(&rec);
     
      // Initialize DC
      CClientDC dc(showPlace);
      CDC bmDC;
      bmDC.CreateCompatibleDC(&dc);
      CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

      BITMAP  bi;
      bmp.GetBitmap(&bi);
      // Scale Image and show full on DC
      dc.StretchBlt(0,0,
            rec.Width(), rec.Height(),
            &bmDC,0,0,bmpInfo.bmWidth, bmpInfo.bmHeight,SRCCOPY);
bmDC.SelectObject(pOldbmp);
}

// How to use these functions ?
Example:

// Path of image
CString path("C:\\cLena.bmp");
// Load bitmap
HBITMAP hBmp = LoadBitmapFromFile(path);
// Get Handle of Item
CWnd* mP = GetDlgItem(IDC_STATIC);
// Show bitmap on Item
ShowBitmapOnDC(hBmp,mP);

How to Convert IplImage* to HBITMAP

Some time you want to show the picture and the result picture that process by OpenCV (IplImage*) on MFC form, so you must convert IplImage* to HBITMAP. I think have many way to do that, but this is a simplest way, you can try it:

HBITMAP ImageShower::ConvertIplImage2HBITMAP(IplImage* pImage)
{
      IplImage* image = (IplImage*)pImage;
      bool imgConverted = false;
      /*if(pImage->nChannels!=3)
      {
            IplImage* imageCh3 = cvCreateImage(cvGetSize(pImage),
                  8, 3);
            if(pImage->nChannels==1)
                  cvCvtColor(pImage, imageCh3, CV_GRAY2RGB);
            image = imageCh3;

            imgConverted = true;
      }*/

      int bpp = image->nChannels * 8;
      assert(image->width >= 0 && image->height >= 0 &&
            (bpp == 8 || bpp == 24 || bpp == 32));
      CvMat dst;
      void* dst_ptr = 0;
      HBITMAP hbmp = NULL;
      unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
      BITMAPINFO* bmi = (BITMAPINFO*)buffer;
      BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

      ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
      bmih->biSize = sizeof(BITMAPINFOHEADER);
      bmih->biWidth = image->width;
      bmih->biHeight = image->origin ? abs(image->height) :
            -abs(image->height);
      bmih->biPlanes = 1;
      bmih->biBitCount = bpp;
      bmih->biCompression = BI_RGB;

      if (bpp == 8) {
            RGBQUAD* palette = bmi->bmiColors;
            int i;
            for (i = 0; i < 256; i++) {
                  palette[i].rgbRed = palette[i].rgbGreen = palette
                        [i].rgbBlue
                        = (BYTE)i;
                  palette[i].rgbReserved = 0;
            }
      }

      hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0,
            0);
      cvInitMatHeader(&dst, image->height, image->width, CV_8UC3,
            dst_ptr, (image->width * image->nChannels + 3) & -4);
      cvConvertImage(image, &dst, image->origin ? CV_CVTIMG_FLIP : 0);

      if(imgConverted)
            cvReleaseImage(&image);

      return hbmp;
}