array[byte] to HBITMAP or CBitmap

前端 未结 4 1563
北恋
北恋 2020-12-06 02:32

I have an array of bytes (which I read through a stream directly from a .bmp and then store as a BLOB in a database) which I want to display as icons in a CImageList. Theref

相关标签:
4条回答
  • 2020-12-06 03:07

    Something like this worked for me:

    
       int bitmap[WX*WY];  // truecolor bitmap data
       BITMAPINFO bm = { sizeof(BITMAPINFOHEADER), WX, WY, 1, 32, BI_RGB, 0, 0, 0, 0, 0 };
       HBITMAP bmp = CreateDIBSection( GetDC(win), &bm, DIB_RGB_COLORS, (void**)&bitmap, 0,0 );
    

    (This is specifically configured for 32-bit colors, but you can specify any kind).

    0 讨论(0)
  • 2020-12-06 03:13

    Ok, here's a complete example: http://nishi.dreamhosters.com/u/so_bmp_v0.zip

    #include <stdio.h>
    #include <windows.h>
    
    #pragma comment(lib,"gdi32.lib")
    #pragma comment(lib,"user32.lib")
    
    char buf[1<<22];
    
    int main( int argc, char **argv ) {
    
      FILE* f = fopen( "winnt.bmp", "rb" ); if( f==0 ) return 1;
      fread( buf, 1,sizeof(buf), f );
      fclose(f);
    
      BITMAPFILEHEADER& bfh = (BITMAPFILEHEADER&)buf[0];
      BITMAPINFO& bi = (BITMAPINFO&)buf[sizeof(BITMAPFILEHEADER)];
      BITMAPINFOHEADER& bih = bi.bmiHeader; 
      char* bitmap = &buf[bfh.bfOffBits];
    
      int WX=1024, WY=512; // window's width/height
      int SX=bih.biWidth, SY=bih.biHeight;
    
      HWND win = CreateWindow( "STATIC", "Bitmap test", 0x90C0, 0,0, WX,WY, 0,0, GetModuleHandle(0), 0 );
    
      MSG msg;
      PAINTSTRUCT ps;
      HDC DC = GetDC(win); // window's DC
      HBITMAP dib = CreateDIBitmap( DC, &bih, CBM_INIT, bitmap, &bi, DIB_RGB_COLORS );
      HDC dibDC = CreateCompatibleDC( DC ); SelectObject( dibDC, dib );
    
      ShowWindow( win, SW_SHOWNOACTIVATE );
      SetFocus( win );
    
      while( GetMessage(&msg,win,0,0) ) {
        int m = msg.message;
        if( m==WM_PAINT ) {
          DC = BeginPaint( win, &ps );
          StretchBlt( DC, 0,0,WX,WY, dibDC,0,0,SX,SY, SRCCOPY );
          EndPaint( win, &ps );
        } else if( (m==WM_KEYDOWN) || (m==WM_SYSKEYDOWN) ) {
          break; 
        } else {
          DispatchMessage(&msg);
        }
      }
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-06 03:14

    Assuming you have the information loaded into a BYTE array named bytes....

    BITMAPFILEHEADER* bmfh;
    bmfh = (BITMAPFILEHEADER*)bytes;
    
    BITMAPINFOHEADER* bmih;
    bmih = (BITMAPINFOHEADER*)(bytes + sizeof(BITMAPFILEHEADER));
    BITMAPINFO* bmi;
    bmi = (BITMAPINFO*)bmih;
    
    void* bits;
    bits = (void*)(bytes + bmfh->bfOffBits);
    
    HDC hdc = ::GetDC(NULL);
    
    HBITMAP hbmp = CreateDIBitmap(hdc, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS) ;
    
    ::ReleaseDC(NULL, hdc);
    

    It's a little messy and could use a hefty dose of error checking, but the basic idea is sound.

    0 讨论(0)
  • 2020-12-06 03:15

    Following sample could help you.

    BITMAPINFO bmInfo;
    BITMAPINFOHEADER &bmInfohdr = (BITMAPINFOHEADER)bmInfo.bmiHeader;
    
    bmInfohdr.biSize = 40 + 255; //I think it's not of use
    bmInfohdr.biWidth = x;
    bmInfohdr.biHeight = y;
    bmInfohdr.biPlanes=1;
    bmInfohdr.biBitCount=8;
    bmInfohdr.biCompression=0;
    bmInfohdr.biSizeImage=0;
    bmInfohdr.biXPelsPerMeter = 0;
    bmInfohdr.biYPelsPerMeter = 0;
    bmInfohdr.biClrUsed = 0;
    bmInfohdr.biClrImportant = 0;
    
               // should I allocate memory further than the 
               // bmColors[1]?? anyway the compiler gives an
               // error for type mismatch!
    //bmInfo.bmiColors = (RGBQUAD *) 
                      malloc(sizeof(RGBQUAD) * 256);
    
    // here I define the 256 graylevel palette
    for (int i=0; i<256; i++)
    {
       bmInfo.bmiColors[i].rgbRed = i;
       bmInfo.bmiColors[i].rgbGreen = i;
       bmInfo.bmiColors[i].rgbBlue = i;
    }
    
    
    BYTE *matrix;
    matrix = (BYTE*)malloc(size*sizeof(BYTE));
    // here I put the BYTE values of the pixels
    
    CDC *pdcDest = this->GetDC();
    
    HBITMAP hBmp = CreateDIBitmap( pdcDest->m_hDC,
                    &bmInfohdr,
                    CBM_INIT,
                    matrix,    
                    &bmInfo,
                    DIB_RGB_COLORS);
    m_bmpBitmap.Attach( hBmp );
    
    0 讨论(0)
提交回复
热议问题