Click here to Skip to main content
11,631,340 members (75,892 online)
Click here to Skip to main content

Tagged as

Create bitmap from pixels

, 25 Jan 2011 CPOL 19.5K 7
Rate this:
Please Sign up or sign in to vote.
Create a bitmap from an array of pixels
static HBITMAP Create8bppBitmap(HDC hdc, int width, int height, LPVOID pBits = NULL)
{
    BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
    BITMAPINFOHEADER &bih(bmi->bmiHeader);
    bih.biSize = sizeof (BITMAPINFOHEADER);
    bih.biWidth         = width;
    bih.biHeight        = -height;
    bih.biPlanes        = 1;
    bih.biBitCount      = 8;
    bih.biCompression   = BI_RGB;
    bih.biSizeImage     = 0;
    bih.biXPelsPerMeter = 14173;
    bih.biYPelsPerMeter = 14173;
    bih.biClrUsed       = 0;
    bih.biClrImportant  = 0;
    for (int I = 0; I <= 255; I++)
    {
        bmi->bmiColors[I].rgbBlue = bmi->bmiColors[I].rgbGreen = bmi->bmiColors[I].rgbRed = (BYTE)I;
        bmi->bmiColors[I].rgbReserved = 0;
    }
 
    void *Pixels = NULL;
    HBITMAP hbmp = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &Pixels, NULL, 0);
 
    if(pBits != NULL)
    {
        //fill the bitmap
        BYTE* pbBits = (BYTE*)pBits;
        BYTE *Pix = (BYTE *)Pixels;
        memcpy(Pix, pbBits, width * height);
    }
 
    free(bmi);
 
    return hbmp;
}
 
static HBITMAP CreateBitmapFromPixels( HDC hDC, UINT uWidth, UINT uHeight, UINT uBitsPerPixel, LPVOID pBits)
    {
           if(uBitsPerPixel < 8) // NOT IMPLEMENTED YET
              return NULL;
 
          if(uBitsPerPixel == 8)
              return Create8bppBitmap(hDC, uWidth, uHeight, pBits);
 
          HBITMAP hBitmap = 0;
          if ( !uWidth || !uHeight || !uBitsPerPixel )
             return hBitmap;
          LONG lBmpSize = uWidth * uHeight * (uBitsPerPixel/8) ;
          BITMAPINFO bmpInfo = { 0 };
          bmpInfo.bmiHeader.biBitCount = uBitsPerPixel;
          bmpInfo.bmiHeader.biHeight = uHeight;
          bmpInfo.bmiHeader.biWidth = uWidth;
          bmpInfo.bmiHeader.biPlanes = 1;
          bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
               // Pointer to access the pixels of bitmap
          UINT * pPixels = 0;
          hBitmap = CreateDIBSection( hDC, (BITMAPINFO *)&
                                      bmpInfo, DIB_RGB_COLORS, (void **)&
                                      pPixels , NULL, 0);
 
          if ( !hBitmap )
              return hBitmap; // return if invalid bitmaps

          //SetBitmapBits( hBitmap, lBmpSize, pBits);
          // Directly Write
          memcpy(pPixels, pBits, lBmpSize );
 
          return hBitmap;
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
mOOmOOn_1-Aug-13 22:40
membermOOmOOn_1-Aug-13 22:40 
GeneralYes, I know these are all in the internet.. but, when u post... Pin
Mukit, Ataul25-Jan-11 0:25
memberMukit, Ataul25-Jan-11 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150723.1 | Last Updated 25 Jan 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid