Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bmp

Bmp

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dodoxor
    wrote on last edited by
    #1

    Hi to everyone, i have this code that tansform an IlpImage* in a bmp format.. char* bitmap_buf; IplImage* image; BITMAPINFOHEADER InfoBitmapHdr; BITMAPFILEHEADER BitmapHdr; image=cvLoadImage("nome_file.ext",1); memset(&InfoBitmapHdr, 0, sizeof(BITMAPINFOHEADER)); memset(&BitmapHdr, 0, sizeof(BITMAPFILEHEADER)); InfoBitmapHdr.biWidth = image->width; InfoBitmapHdr.biHeight = image->height; InfoBitmapHdr.biSize = sizeof(BITMAPINFOHEADER); InfoBitmapHdr.biPlanes = 1; InfoBitmapHdr.biBitCount = 24; InfoBitmapHdr.biSizeImage = image->imageSize; BitmapHdr.bfType = 0x4d42; BitmapHdr.bfSize = (DWORD) image->imageSize + sizeof(BITMAPFILEHEADER); BitmapHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + InfoBitmapHdr.biClrUsed * sizeof (RGBQUAD); bitmap_buf= new char[image->imageSize]; memcpy( bitmap_buf, (char*)&BitmapHdr, sizeof(BITMAPFILEHEADER)); memcpy( bitmap_buf + sizeof(BITMAPFILEHEADER), (char*)&InfoBitmapHdr, sizeof(BITMAPINFOHEADER)); memcpy( bitmap_buf + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), (char*)image->imageData, image->imageSize - 60); Can you tell me which variable must i use to visualize the bmp in a picture box of a window form application in visual c++?? Thanks for answers, regards.

    M 1 Reply Last reply
    0
    • D dodoxor

      Hi to everyone, i have this code that tansform an IlpImage* in a bmp format.. char* bitmap_buf; IplImage* image; BITMAPINFOHEADER InfoBitmapHdr; BITMAPFILEHEADER BitmapHdr; image=cvLoadImage("nome_file.ext",1); memset(&InfoBitmapHdr, 0, sizeof(BITMAPINFOHEADER)); memset(&BitmapHdr, 0, sizeof(BITMAPFILEHEADER)); InfoBitmapHdr.biWidth = image->width; InfoBitmapHdr.biHeight = image->height; InfoBitmapHdr.biSize = sizeof(BITMAPINFOHEADER); InfoBitmapHdr.biPlanes = 1; InfoBitmapHdr.biBitCount = 24; InfoBitmapHdr.biSizeImage = image->imageSize; BitmapHdr.bfType = 0x4d42; BitmapHdr.bfSize = (DWORD) image->imageSize + sizeof(BITMAPFILEHEADER); BitmapHdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + InfoBitmapHdr.biClrUsed * sizeof (RGBQUAD); bitmap_buf= new char[image->imageSize]; memcpy( bitmap_buf, (char*)&BitmapHdr, sizeof(BITMAPFILEHEADER)); memcpy( bitmap_buf + sizeof(BITMAPFILEHEADER), (char*)&InfoBitmapHdr, sizeof(BITMAPINFOHEADER)); memcpy( bitmap_buf + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), (char*)image->imageData, image->imageSize - 60); Can you tell me which variable must i use to visualize the bmp in a picture box of a window form application in visual c++?? Thanks for answers, regards.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      You have all the info you need to use CreateDIBSection() to create a HBITMAP that can be set to a picture box. Even without a picture box, you could use StretchDIBits() with a BITMAPINFO and a pointer to the image bits to draw the image diectly on a window. Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        You have all the info you need to use CreateDIBSection() to create a HBITMAP that can be set to a picture box. Even without a picture box, you could use StretchDIBits() with a BITMAPINFO and a pointer to the image bits to draw the image diectly on a window. Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        D Offline
        D Offline
        dodoxor
        wrote on last edited by
        #3

        Thanks Mark, i see form msdn the definition of hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits, lpInfo,wUsage); but i can't associate my variable to that are required in the function. For example hdc(where is in my code??), lpinfo( InfoBitmapHdr.biWidth),CBM_INIT,lpbits(??),lpinfo(InfoBitmapHdr.biHeight),wUsage(??) Thanks for answers, regards.

        M 1 Reply Last reply
        0
        • D dodoxor

          Thanks Mark, i see form msdn the definition of hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits, lpInfo,wUsage); but i can't associate my variable to that are required in the function. For example hdc(where is in my code??), lpinfo( InfoBitmapHdr.biWidth),CBM_INIT,lpbits(??),lpinfo(InfoBitmapHdr.biHeight),wUsage(??) Thanks for answers, regards.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          A BITMAPINFOHEADER is part of the BITMAPINFO struct required by CreateDIBSection(). The HDC is only used for bitmaps requiring a color table. The palette selected into the DC is used by CreateDIBSection() to construct a color table when necessary. Since you have 24-bit pixels it's not needed. You can use a screen DC. I haven't used IplImage but based on what I've seen, the imageSize member is the size of just the pixel data, not pixel data with Windows BITMAPINFO stuff attached. If I'm wrong you'll have to adjust the following code a bit :) Here's a modified version of your sample code. Note that this code is hardwired for 24-bit images...

          BYTE* bitmap_buf;
          IplImage* image;
          BITMAPINFO bmi;

          image=cvLoadImage("nome_file.ext",1);

          memset(&bmi, 0, sizeof(BITMAPINFO));
          bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
          bmi.bmiHeader.biWidth = image->width;
          bmi.bmiHeader.biHeight = image->height;
          bmi.bmiHeader.biPlanes = 1;
          bmi.bmiHeader.biBitCount = 24;
          bmi.bmiHeader.biCompression = BI_RGB;
          bmi.bmiHeader.biSizeImage = image->imageSize;
          //bmi.bmiHeader.biXPelsPerMeter = 0;
          //bmi.bmiHeader.biYPelsPerMeter = 0;
          //bmi.bmiHeader.biClrUsed = 0;
          //bmi.bmiHeader.biClrImportant = 0;

          HDC hdc = ::GetDC(0);
          HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &bitmap_buf, NULL, 0);
          ::ReleaseDC(0, hdc);

          if (hBitmap)
          {
          memcpy( bitmap_buf, image->imageData, image->imageSize );

          // do stuff with the new HBITMAP
          ...
          }

          *EDIT* I looked up the IplImage structure. I'm not sure what the default alignment of rows of pixel data is in OpenCV, but for a Windows DIB, rows must be aligned to a DWORD (4-byte) boundary. If this does not match the IplImage alignment (based on image->widthStep) then your resulting bitmap will look skewed :) Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          D 1 Reply Last reply
          0
          • M Mark Salsbery

            A BITMAPINFOHEADER is part of the BITMAPINFO struct required by CreateDIBSection(). The HDC is only used for bitmaps requiring a color table. The palette selected into the DC is used by CreateDIBSection() to construct a color table when necessary. Since you have 24-bit pixels it's not needed. You can use a screen DC. I haven't used IplImage but based on what I've seen, the imageSize member is the size of just the pixel data, not pixel data with Windows BITMAPINFO stuff attached. If I'm wrong you'll have to adjust the following code a bit :) Here's a modified version of your sample code. Note that this code is hardwired for 24-bit images...

            BYTE* bitmap_buf;
            IplImage* image;
            BITMAPINFO bmi;

            image=cvLoadImage("nome_file.ext",1);

            memset(&bmi, 0, sizeof(BITMAPINFO));
            bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            bmi.bmiHeader.biWidth = image->width;
            bmi.bmiHeader.biHeight = image->height;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 24;
            bmi.bmiHeader.biCompression = BI_RGB;
            bmi.bmiHeader.biSizeImage = image->imageSize;
            //bmi.bmiHeader.biXPelsPerMeter = 0;
            //bmi.bmiHeader.biYPelsPerMeter = 0;
            //bmi.bmiHeader.biClrUsed = 0;
            //bmi.bmiHeader.biClrImportant = 0;

            HDC hdc = ::GetDC(0);
            HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &bitmap_buf, NULL, 0);
            ::ReleaseDC(0, hdc);

            if (hBitmap)
            {
            memcpy( bitmap_buf, image->imageData, image->imageSize );

            // do stuff with the new HBITMAP
            ...
            }

            *EDIT* I looked up the IplImage structure. I'm not sure what the default alignment of rows of pixel data is in OpenCV, but for a Windows DIB, rows must be aligned to a DWORD (4-byte) boundary. If this does not match the IplImage alignment (based on image->widthStep) then your resulting bitmap will look skewed :) Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            D Offline
            D Offline
            dodoxor
            wrote on last edited by
            #5

            Thanks for answers,it's very useful. There's a prolem when i try to compile: HBITMAP hBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &bitmap_buf, NULL, 0); generate error C2664: 'CreateDIBSection' : cannot convert parameter 4 from 'BYTE **' to 'void **' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast so i try to do a safe cast, i add this line before the CreateDIBSection void* buff=safe_cast(bitmap_buf); and use &buff instead of bitmap_buf in CreateDIBSection but the result is a list of build error: error LNK2028: unresolved token (0A0002FA) "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC@@$$J18YGHPAUHWND__@@PAUHDC__@@@Z) referenced in function "private: void __clrcall WinCam::Form1::capimgbut_Click(class System::Object ^,class System::EventArgs ^)" (?capimgbut_Click@Form1@WinCam@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) WinCam.obj : error LNK2028: unresolved token (0A0002FB) "extern "C" struct HBITMAP__ * __stdcall CreateDIBSection(struct HDC__ *,struct tagBITMAPINFO const *,unsigned int,void * *,void *,unsigned long)" (?CreateDIBSection@@$$J224YGPAUHBITMAP__@@PAUHDC__@@PBUtagBITMAPINFO@@IPAPAXPAXK@Z) referenced in function "private: void __clrcall WinCam::Form1::capimgbut_Click(class System::Object ^,class System::EventArgs ^)" (?capimgbut_Click@Form1@WinCam@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) ...and so on. I don't know why... Thanks however for help. Regards.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups