CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    [RESOLVED] Read grayscale bitmap into array

    Hello,

    I need to read in an 8 bit grayscale bmp and I want the value of its pixels stored in an array (can do that myself). I can't seem to get the value between 0 and 255, I always get zero with the code below. What's wrong?

    Code:
     HANDLE hBitmap;  //Holds the Handle of the Bitmap once it is loaded.
     HDC hDC;  //Our temporary Device Context, which will be used to hold our Bitmap later on.
    
     hDC = CreateCompatibleDC(NULL);  //Create's a compatible Device Context(DC) and stores its value in 'hDC'.
    
     hBitmap = LoadImage(0,"E:\\Test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //This loads our Image into memory and stores the handle in the variable "hBitmap". You may change the Directory from this API call to one containing your Image.
    
     //Get info about the bitmap
     
     BITMAP Bitmap;
     SIZE ImageSize;
    
     //Get info about the bitmap
     GetObject(hBitmap, sizeof(Bitmap), &Bitmap);
     ImageSize.cx=Bitmap.bmWidth;
     ImageSize.cy=Bitmap.bmHeight;
    
     SelectObject(hDC,hBitmap); //Puts our loaded Image into our temporary Device Context.
    
     for(int x=0;x<ImageSize.cx;++x)
     for(int y=0;y<ImageSize.cy;++y)
     {
      COLORREF color=GetPixel(hDC,x,y);
    
      // Problem here : color is always zero, I want the correct value and put it in an array
     }
    
     // Clean up
    
     DeleteDC(hDC);
    Last edited by Simon666; August 26th, 2010 at 07:16 AM. Reason: Managed to find something that works for me
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Read grayscale bitmap into array

    Quote Originally Posted by Simon666 View Post
    I need to read in an 8 bit grayscale bmp and I want the value of its pixels stored in an array (can do that myself). I can't seem to get the value between 0 and 255, I always get zero with the code below. What's wrong?
    Could you post your test bitmap here?
    How do you "always get zero"? What's your test?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: Read grayscale bitmap into array

    As test I use a 256 color bitmap that has a grayscale palette. I change some pixels in all four corners to various grayscales but always get zero no matter what.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Read grayscale bitmap into array

    Quote Originally Posted by Simon666 View Post
    As test I use a 256 color bitmap that has a grayscale palette. I change some pixels in all four corners to various grayscales but always get zero no matter what.
    I am sorry, but you have NOT answered Vlad's question:

    Quote Originally Posted by VladimirF View Post
    Could you post your test bitmap here?
    How do you "always get zero"? What's your test?
    Since we still don't know how and what exactly you checked that "always get zero" (did you debug your code, BTW?) we cannot help you more.
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: Read grayscale bitmap into array

    Quote Originally Posted by VictorN View Post
    I am sorry, but you have NOT answered Vlad's question:

    Since we still don't know how and what exactly you checked that "always get zero" (did you debug your code, BTW?) we cannot help you more.
    Fine, I didn't want to upload it as attachment as it is several hundred kilobyte, so I put it on my own website. If you want it, it's here:

    http://users.ugent.be/~skdmeule/Test.bmp

    With regards to the second part, if I do:

    CString Test; Test.Format("%d",color);
    AfxMessageBox(Test);

    in the for-loop I always get zero. So I am pretty sure of that.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  6. #6
    Join Date
    Apr 2009
    Posts
    598

    Re: Read grayscale bitmap into array

    1. Get the right dimensions
    I use a more complex code than yours:
    Code:
    SIZE hbmp_size;
    
             GetBitmapDimensionEx(hBitmap, &hbmp_size);
             ImageSize.cx = (UINT) hbmp_size.cx;
             ImageSize.cy = (UINT) hbmp_size.cy;
             if (ImageSize.cx == 0 || ImageSize.cy == 0) {
                BITMAP bm;
                if (GetObject(hBitmap, sizeof(BITMAP), &Bitmap, (LPSTR)&Bitmap) != 0) {
                   ImageSize.cx = (UINT) Bitmap.bmWidth;
                   ImageSize.cy = (UINT) Bitmap.bmHeight;
                }
             }
    2. Have a buffer for your bitmap bits
    I always allocate a buffer for the bitmap bits. Maybe this is not useful some situations. Anyway it works for me.

    3. Copy the grid to your buffer with GetDIBits()

    The code is quite long. But you will find examples with gloogloo.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Read grayscale bitmap into array

    Quote Originally Posted by Simon666 View Post
    With regards to the second part, if I do:

    CString Test; Test.Format("%d",color);
    AfxMessageBox(Test);

    in the for-loop I always get zero. So I am pretty sure of that.
    What do you mean - always? Your bitmap has 900,000 pixels. Are you saying that you read and OK'ed 900,000 message boxes???
    Anyway, your loops scan the bitmap vertically, and your first vertical line is all black. The white dots are not really at the corners.
    Note that for better performance (which is not really an issue with the very slow GetPixel), you should scan your bitmap horizontally.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: Read grayscale bitmap into array

    Quote Originally Posted by VladimirF View Post
    What do you mean - always? Your bitmap has 900,000 pixels. Are you saying that you read and OK'ed 900,000 message boxes???
    Ofcourse not, but I know some values in all 4 corners. Anyway, I managed to solve it myself looking at some related code on the net. For posterity reasons:

    Code:
     // Additional functions needed
    int GetCBitmapWidth(const CBitmap & cbm)
    {
     BITMAP bm;
     cbm.GetObject(sizeof(BITMAP),&bm);
     return bm.bmWidth;
    }
    
    int GetCBitmapHeight(const CBitmap & cbm)
    {
     BITMAP bm;
     cbm.GetObject(sizeof(BITMAP),&bm);
     return bm.bmHeight;
    }
    
     // Actual code
    
     CDC *dc=new CDC;
     dc->CreateCompatibleDC(NULL);
    
     CBitmap bitmap;
    
     bitmap.m_hObject=(HBITMAP)::LoadImage(0,"E:\\Test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    
     dc->SelectObject(bitmap);
     
     int cx = GetCBitmapWidth(bitmap);
     int cy = GetCBitmapHeight(bitmap);
    
     for(int x=0;x<ImageSize.cx;++x)
     for(int y=0;y<ImageSize.cy;++y)
     {
      COLORREF color=dc->GetPixel(x,y);
    
      // At least this crap works, so now I can do my image processing
     }
    
     delete dc;
    CBitmap doesn't have member functions to determine its width and height as on MSDN, that's why you need those helper functions.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Read grayscale bitmap into array

    Quote Originally Posted by Simon666 View Post
    Ofcourse not, but I know some values in all 4 corners. Anyway, I managed to solve it myself looking at some related code on the net. For posterity reasons:...

    CBitmap doesn't have member functions to determine its width and height as on MSDN, that's why you need those helper functions.
    As I mentioned before, the corners of your image are black.
    Glad that you solved your problem.
    However, for clarity, I have to say that:
    1. There are no significant differences between this code and the fragment from your first post.
    2. The use of CBitmap is unjustified: you do not use any of its methods and simply store HGDIOBJ in it.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Read grayscale bitmap into array

    I agree with Vlad with one slight exception: using CBitmap rather than HBITMAP helps to forget about deleting HBITMAP object.

    And I'd like to also add, dear Simon666, that there is no any advantage in creating CDC object in the HEAP. A very simple
    Code:
    CDC dc;
    must work the same way and doesn't need to call delete.
    Victor Nijegorodov

  11. #11
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Read grayscale bitmap into array

    It's probably also worth noting that using LoadImage() without the LR_CREATEDIBSECTION flag will result in mapping the bitmap to the display colors of the default device. I haven't checked the code, but this may result in a monochrome bitmap since the DC is initialized with CreateCompatibleDC(NULL). This might be why it's "all black".

  12. #12
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: Read grayscale bitmap into array

    I know things can be done better. That's why I'm asking help here, I'm not a programmer by education, but I need something that does the job. I managed to concoct something myself using related googled code and it seems it can be done better, but hey, I have my results now and that's what matters.

    Quote Originally Posted by VladimirF
    As I mentioned before, the corners of your image are black.
    Of course I tested it editing the corners to some variations in shade.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured