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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating a Bitmap from a Framegrabber Image

Creating a Bitmap from a Framegrabber Image

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsdata-structurestutorial
11 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.
  • I Offline
    I Offline
    im79
    wrote on last edited by
    #1

    Hello all, i'm trying to display a Bitmap in a Picture Control. The picture is captured by a CMOS camera (gray), and all i have is a pointer to the image in the Framegrabber buffer. The problem is that i don't have a real bitmap in that buffer, just an array with pixel values. The technical support of the Framgrabber producer couldn't help me till now and i'm still waiting for thier reply :sigh:. I was trying to create a Bitmap manualy and fill in all the needed structures like BITMAPINFO and so on but nothing worked. Can you tell me how to construct a bitmap from an pixelarray, just knowing the lenght, width and that's it is gray scale picture. I would be very greatfull for any kind of help! I wish you a nice Weekend :) im79

    J 1 Reply Last reply
    0
    • I im79

      Hello all, i'm trying to display a Bitmap in a Picture Control. The picture is captured by a CMOS camera (gray), and all i have is a pointer to the image in the Framegrabber buffer. The problem is that i don't have a real bitmap in that buffer, just an array with pixel values. The technical support of the Framgrabber producer couldn't help me till now and i'm still waiting for thier reply :sigh:. I was trying to create a Bitmap manualy and fill in all the needed structures like BITMAPINFO and so on but nothing worked. Can you tell me how to construct a bitmap from an pixelarray, just knowing the lenght, width and that's it is gray scale picture. I would be very greatfull for any kind of help! I wish you a nice Weekend :) im79

      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      Gray as in 8-bits per pixel? Did you set up the color table? Show how you're calling CreateDIBitmap and your input values (BITMAPINFO).

      I 1 Reply Last reply
      0
      • J Justin Tay

        Gray as in 8-bits per pixel? Did you set up the color table? Show how you're calling CreateDIBitmap and your input values (BITMAPINFO).

        I Offline
        I Offline
        im79
        wrote on last edited by
        #3

        Hello all, hello hrfy and thanks for your answer :) Yes it is 8-bits per pixel. I didn't set the color table. I just called CreateBitmap or DIBitmap passing width, length, the pointer to the pixel array and so on. I as i explaind in my first posting, i don't have a BITMAPINFO, can make one manualy? :confused: I tried manythings but nothing worked. I'm new to windows programming and MFC. I'm not at work now, so i don't have the source code here right now. Eventhough i cann't provide enough info, i hope u can still help me on this one :~ Kind regards and thanks again , im79

        J 1 Reply Last reply
        0
        • I im79

          Hello all, hello hrfy and thanks for your answer :) Yes it is 8-bits per pixel. I didn't set the color table. I just called CreateBitmap or DIBitmap passing width, length, the pointer to the pixel array and so on. I as i explaind in my first posting, i don't have a BITMAPINFO, can make one manualy? :confused: I tried manythings but nothing worked. I'm new to windows programming and MFC. I'm not at work now, so i don't have the source code here right now. Eventhough i cann't provide enough info, i hope u can still help me on this one :~ Kind regards and thanks again , im79

          J Offline
          J Offline
          Justin Tay
          wrote on last edited by
          #4

          im79 wrote:

          i don't have a BITMAPINFO, can make one manualy?

          If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.

          HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
          {
          BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
          ZeroMemory(pbmi, sizeof(BITMAPINFO));
          pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
          pbmi->bmiHeader.biWidth = nWidth;
          pbmi->bmiHeader.biHeight = -nHeight;
          pbmi->bmiHeader.biPlanes = 1;
          pbmi->bmiHeader.biBitCount = 8;
          pbmi->bmiHeader.biCompression = BI_RGB;
          pbmi->bmiHeader.biSizeImage = nWidth * nHeight;

          RGBQUAD *pColors = pbmi->bmiColors;
          for(int i = 0; i < 256; i++)
          {
          pColors->rgbBlue = i;
          pColors->rgbGreen = i;
          pColors->rgbRed = i;
          pColors->rgbReserved = 0;
          pColors++;
          }

          CWindowDC dc(NULL);
          HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);

          delete pbmi;

          return hBitmap;
          }

          This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.

          I 2 Replies Last reply
          0
          • J Justin Tay

            im79 wrote:

            i don't have a BITMAPINFO, can make one manualy?

            If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.

            HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
            {
            BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
            ZeroMemory(pbmi, sizeof(BITMAPINFO));
            pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
            pbmi->bmiHeader.biWidth = nWidth;
            pbmi->bmiHeader.biHeight = -nHeight;
            pbmi->bmiHeader.biPlanes = 1;
            pbmi->bmiHeader.biBitCount = 8;
            pbmi->bmiHeader.biCompression = BI_RGB;
            pbmi->bmiHeader.biSizeImage = nWidth * nHeight;

            RGBQUAD *pColors = pbmi->bmiColors;
            for(int i = 0; i < 256; i++)
            {
            pColors->rgbBlue = i;
            pColors->rgbGreen = i;
            pColors->rgbRed = i;
            pColors->rgbReserved = 0;
            pColors++;
            }

            CWindowDC dc(NULL);
            HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);

            delete pbmi;

            return hBitmap;
            }

            This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.

            I Offline
            I Offline
            im79
            wrote on last edited by
            #5

            Hi, i've tried your code in my appicaltion and have another problem now :sigh:. I need a CBitmap object, so i tried the following: CDC *pDC = GetDC(); CBitmap* myBitmap; myBitmap = CBitmap::FromHandle(hBitmap); myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog I get an Unhandled exception in wingdi.cpp line 1102: BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); // <= line 1102 if (hObject == NULL) return FALSE; CHandleMap* pMap = afxMapHGDIOBJ(TRUE); ASSERT(pMap != NULL); pMap->SetPermanent(m_hObject = hObject, this); return TRUE; } This is the call stack: >mfc71d.dll!CGdiObject::Attach(void * hObject=0x0305061d) Line 1102 + 0x1c C++ mfc71d.dll!CBitmap::CreateCompatibleBitmap(CDC * pDC=0x00816af4, int nWidth=400, int nHeight=400) Line 217 + 0x25 C++ I hope you can help me again here:) im79

            J 1 Reply Last reply
            0
            • I im79

              Hi, i've tried your code in my appicaltion and have another problem now :sigh:. I need a CBitmap object, so i tried the following: CDC *pDC = GetDC(); CBitmap* myBitmap; myBitmap = CBitmap::FromHandle(hBitmap); myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog I get an Unhandled exception in wingdi.cpp line 1102: BOOL CGdiObject::Attach(HGDIOBJ hObject) { ASSERT(m_hObject == NULL); // <= line 1102 if (hObject == NULL) return FALSE; CHandleMap* pMap = afxMapHGDIOBJ(TRUE); ASSERT(pMap != NULL); pMap->SetPermanent(m_hObject = hObject, this); return TRUE; } This is the call stack: >mfc71d.dll!CGdiObject::Attach(void * hObject=0x0305061d) Line 1102 + 0x1c C++ mfc71d.dll!CBitmap::CreateCompatibleBitmap(CDC * pDC=0x00816af4, int nWidth=400, int nHeight=400) Line 217 + 0x25 C++ I hope you can help me again here:) im79

              J Offline
              J Offline
              Justin Tay
              wrote on last edited by
              #6

              im79 wrote:

              myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight);

              What are you trying to do here?

              im79 wrote:

              myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog

              The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...

              I 1 Reply Last reply
              0
              • J Justin Tay

                im79 wrote:

                myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight);

                What are you trying to do here?

                im79 wrote:

                myBitmap->LoadBitmap(IDC_STATIC_PICTURE);//picture control in my dialog

                The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...

                I Offline
                I Offline
                im79
                wrote on last edited by
                #7

                hfry wrote:

                im79 wrote: myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); What are you trying to do here?

                I thought that i have to convert the DIB to a DDB Bitmap, so i guess that's wrong :~ I just want to display the Bitmap in a picture control, but it's not as easy as i thought

                hfry wrote:

                The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...

                Yes that's stupid, I should have read the MSDN manuals

                J 1 Reply Last reply
                0
                • I im79

                  hfry wrote:

                  im79 wrote: myBitmap->CreateCompatibleBitmap(pDC, m_iWidth, m_iHeight); What are you trying to do here?

                  I thought that i have to convert the DIB to a DDB Bitmap, so i guess that's wrong :~ I just want to display the Bitmap in a picture control, but it's not as easy as i thought

                  hfry wrote:

                  The CBitmap::LoadBitmap function is used to load an image into the bitmap, not set an image into a picture control...

                  Yes that's stupid, I should have read the MSDN manuals

                  J Offline
                  J Offline
                  Justin Tay
                  wrote on last edited by
                  #8

                  im79 wrote:

                  it's not as easy as i thought

                  It is actually.

                  CStatic *picture = (CStatic*) GetDlgItem(IDC_STATIC_PICTURE);
                  picture->SetBitmap(hBitmap);

                  Make sure the static control's type is actually set to Bitmap.

                  I 1 Reply Last reply
                  0
                  • J Justin Tay

                    im79 wrote:

                    it's not as easy as i thought

                    It is actually.

                    CStatic *picture = (CStatic*) GetDlgItem(IDC_STATIC_PICTURE);
                    picture->SetBitmap(hBitmap);

                    Make sure the static control's type is actually set to Bitmap.

                    I Offline
                    I Offline
                    im79
                    wrote on last edited by
                    #9

                    Hi hfry It's working :-D Thank you very much :rose:

                    1 Reply Last reply
                    0
                    • J Justin Tay

                      im79 wrote:

                      i don't have a BITMAPINFO, can make one manualy?

                      If you know what kind of data is in the pixel array, you should have no problem populating the header. I've just written and tested this by generating a grayscale gradient and it works.

                      HBITMAP CreateGrayscaleBitmap(int nWidth, int nHeight, LPBYTE pData)
                      {
                      BITMAPINFO *pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + (sizeof(RGBQUAD) * 256)];
                      ZeroMemory(pbmi, sizeof(BITMAPINFO));
                      pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                      pbmi->bmiHeader.biWidth = nWidth;
                      pbmi->bmiHeader.biHeight = -nHeight;
                      pbmi->bmiHeader.biPlanes = 1;
                      pbmi->bmiHeader.biBitCount = 8;
                      pbmi->bmiHeader.biCompression = BI_RGB;
                      pbmi->bmiHeader.biSizeImage = nWidth * nHeight;

                      RGBQUAD *pColors = pbmi->bmiColors;
                      for(int i = 0; i < 256; i++)
                      {
                      pColors->rgbBlue = i;
                      pColors->rgbGreen = i;
                      pColors->rgbRed = i;
                      pColors->rgbReserved = 0;
                      pColors++;
                      }

                      CWindowDC dc(NULL);
                      HBITMAP hBitmap = CreateDIBitmap(dc, &pbmi->bmiHeader, CBM_INIT, pData, pbmi, DIB_RGB_COLORS);

                      delete pbmi;

                      return hBitmap;
                      }

                      This is for a top-down bitmap. If your image is flipped, remove the negative sign in front of the height.

                      I Offline
                      I Offline
                      im79
                      wrote on last edited by
                      #10

                      Hi :) i need your (or any one's) help again :) I'm trying for long time to change this code in order to make an 8Bit color Bitmap but it's not working :confused: I'll be greatfull for any help!

                      J 1 Reply Last reply
                      0
                      • I im79

                        Hi :) i need your (or any one's) help again :) I'm trying for long time to change this code in order to make an 8Bit color Bitmap but it's not working :confused: I'll be greatfull for any help!

                        J Offline
                        J Offline
                        Justin Tay
                        wrote on last edited by
                        #11

                        Hi,

                        RGBQUAD *pColors = pbmi->bmiColors;
                        for(int i = 0; i < 256; i++)
                        {
                        pColors->rgbBlue = i;
                        pColors->rgbGreen = i;
                        pColors->rgbRed = i;
                        pColors->rgbReserved = 0;
                        pColors++;
                        }

                        The code snippet above is setting the color table. In your previous question, you wanted it grayscale so the color table so I set it up as such. If you want it to be color you need to setup the appropriate color table that your 8-bit data maps to. Regards, Justin Tay

                        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