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. Accessing Pixels with CreateDIBSection

Accessing Pixels with CreateDIBSection

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelpquestion
6 Posts 3 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.
  • M Offline
    M Offline
    Member 9255349
    wrote on last edited by
    #1

    Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!

        CDC\* pDC=GetDC();
    HDC hDC = \*pDC;
    HDC hDCMem = CreateCompatibleDC(hDC);
    
    BYTE\* lpBitmapBits = NULL; 
    
    BITMAPINFO bi; 
    ZeroMemory(&bi, sizeof(BITMAPINFO));
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = m\_rcMainRect.Width();
    bi.bmiHeader.biHeight = -m\_rcMainRect.Height();
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    
    HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0);
    HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); 
    
    BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY);
    
    
    for(int x=0; x<800; x=x+4)
    {
    	for(int y=0; y<900; y++)
    	{
    	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00;    //alpha channel?
    	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00;
    	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF;
    	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00;
    	}
    }
    
    BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY);
    
        SelectObject(hDCMem,oldbmp);  
        DeleteDC(hDCMem);  
        DeleteObject(bitmap);
    
    E P 2 Replies Last reply
    0
    • M Member 9255349

      Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!

          CDC\* pDC=GetDC();
      HDC hDC = \*pDC;
      HDC hDCMem = CreateCompatibleDC(hDC);
      
      BYTE\* lpBitmapBits = NULL; 
      
      BITMAPINFO bi; 
      ZeroMemory(&bi, sizeof(BITMAPINFO));
      bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
      bi.bmiHeader.biWidth = m\_rcMainRect.Width();
      bi.bmiHeader.biHeight = -m\_rcMainRect.Height();
      bi.bmiHeader.biPlanes = 1;
      bi.bmiHeader.biBitCount = 32;
      
      HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0);
      HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); 
      
      BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY);
      
      
      for(int x=0; x<800; x=x+4)
      {
      	for(int y=0; y<900; y++)
      	{
      	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00;    //alpha channel?
      	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00;
      	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF;
      	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00;
      	}
      }
      
      BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY);
      
          SelectObject(hDCMem,oldbmp);  
          DeleteDC(hDCMem);  
          DeleteObject(bitmap);
      
      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      The width of the bitmap is in pixels (DWORDs) and your buffer is in BYTEs ... :) PS: You can use all GDI functions in the memory-DC as well.

      They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

      M 1 Reply Last reply
      0
      • E Eugen Podsypalnikov

        The width of the bitmap is in pixels (DWORDs) and your buffer is in BYTEs ... :) PS: You can use all GDI functions in the memory-DC as well.

        They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

        M Offline
        M Offline
        Member 9255349
        wrote on last edited by
        #3

        Thanks so much for the quick reply. You are a lifesaver! (Or... at least a sleep saver.) :)

        1 Reply Last reply
        0
        • M Member 9255349

          Hello all. I am new to image processing and am having trouble with CreateDIBSection. I have been reading other threads on this topic and code snippets for several days now, so I was hoping at this point to get specific help with my code rather than a link to a previous explanation or code. I have a dialog based app. I want to take a screen shot, change some of the pixel colors and BitBlt it back. In my attempt to make a rectangle of red pixels (or any color) that is 800 pixels in width and 900 in height, I end up with 4 rectangles that are 200 pixels in width and 225 pixels in height, that are separated by 36 pixels and that are alternating is color between red a blue. I'm guessing that this has something to do with the padding. I'd really appreciate an help you can offer!

              CDC\* pDC=GetDC();
          HDC hDC = \*pDC;
          HDC hDCMem = CreateCompatibleDC(hDC);
          
          BYTE\* lpBitmapBits = NULL; 
          
          BITMAPINFO bi; 
          ZeroMemory(&bi, sizeof(BITMAPINFO));
          bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
          bi.bmiHeader.biWidth = m\_rcMainRect.Width();
          bi.bmiHeader.biHeight = -m\_rcMainRect.Height();
          bi.bmiHeader.biPlanes = 1;
          bi.bmiHeader.biBitCount = 32;
          
          HBITMAP bitmap = ::CreateDIBSection(hDCMem, &bi, DIB\_RGB\_COLORS, (LPVOID\*)&lpBitmapBits, NULL, 0);
          HGDIOBJ oldbmp = ::SelectObject(hDCMem, bitmap); 
          
          BitBlt(hDCMem, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDC, 0, 0, SRCCOPY);
          
          
          for(int x=0; x<800; x=x+4)
          {
          	for(int y=0; y<900; y++)
          	{
          	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x\] = 0x00;    //alpha channel?
          	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+1\] = 0x00;
          	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+2\] = 0xFF;
          	lpBitmapBits\[(y\*m\_rcMainRect.Width())+x+3\] = 0x00;
          	}
          }
          
          BitBlt(hDC, 0, 0, m\_rcMainRect.Width(), m\_rcMainRect.Height(), hDCMem, 0, 0, SRCCOPY);
          
              SelectObject(hDCMem,oldbmp);  
              DeleteDC(hDCMem);  
              DeleteObject(bitmap);
          
          P Offline
          P Offline
          pasztorpisti
          wrote on last edited by
          #4

          A good advice for the future: The size in bytes occupied by one row of pixels in memory is called stride. Its important that in bitmaps stride is always a multiple of 4 bytes. In your cases this condition is true in all cases regardless of the bitmap width because 1 pixel occupies 4 bytes. But for example in a 8 bit bitmap (256 colors) with width==3 the stride would be 4 bytes regardless of the actual size being 3 bytes, there would go 1 byte extra padding. stride = ((width * bits_per_pixel + 31) >> 3) & ~3;

          M 1 Reply Last reply
          0
          • P pasztorpisti

            A good advice for the future: The size in bytes occupied by one row of pixels in memory is called stride. Its important that in bitmaps stride is always a multiple of 4 bytes. In your cases this condition is true in all cases regardless of the bitmap width because 1 pixel occupies 4 bytes. But for example in a 8 bit bitmap (256 colors) with width==3 the stride would be 4 bytes regardless of the actual size being 3 bytes, there would go 1 byte extra padding. stride = ((width * bits_per_pixel + 31) >> 3) & ~3;

            M Offline
            M Offline
            Member 9255349
            wrote on last edited by
            #5

            Thank you!

            P 1 Reply Last reply
            0
            • M Member 9255349

              Thank you!

              P Offline
              P Offline
              pasztorpisti
              wrote on last edited by
              #6

              You are welcome!

              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