数组[字节] 到 HBITMAP 或 CBitmap

发布于 2024-10-17 02:57:07 字数 460 浏览 4 评论 0原文

我有一个字节数组(我直接从 .bmp 通过流读取该数组,然后将其作为 BLOB 存储在数据库中),我希望将其显示为 CImageList 中的图标。因此我想以某种方式将我的数据加载到 HBITMAP 或 CBitmap 中。到目前为止,我已经这样做了,从文件中读取:

hPic = (HBITMAP)LoadImage(NULL, strPath, IMAGE_BITMAP, dwWidth, dwHeight, LR_LOADFROMFILE | LR_VGACOLOR);
...
CBitmap bitmap;
bitmap.Attach(hPicRet);

但显然,这只适用于文件,但不适用于字节数组。如何获得相同的结果,但从字节数组中读取?

编辑: 请注意,我的数组不仅包含颜色信息,还包含写入磁盘上的完整文件,包括所有标头和元数据。在我看来,丢弃所有这些信息是一个坏主意。

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. Therefore I want to somehow load my data into an HBITMAP or CBitmap. I have done it like this up to now, reading from a file:

hPic = (HBITMAP)LoadImage(NULL, strPath, IMAGE_BITMAP, dwWidth, dwHeight, LR_LOADFROMFILE | LR_VGACOLOR);
...
CBitmap bitmap;
bitmap.Attach(hPicRet);

But obviously, that only works for files, but not for byte-arrays. How can I get the same result, but reading from an array of byte?

Edit:
Note that my array does not contain just the colour information, but rather the complete file as it is written on disk, including all headers and meta-data. It seems to me that discarding all that information is a bad idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨夜星沙 2024-10-24 02:57:07

假设您已将信息加载到名为 bytes 的 BYTE 数组中......

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);

这有点混乱,可以使用大量的错误检查,但基本思想是合理的。

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.

郁金香雨 2024-10-24 02:57:07

以下示例可以帮助您。

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 );

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 );
浊酒尽余欢 2024-10-24 02:57:07

像这样的东西对我有用:(


   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 );

这是专门为 32 位颜色配置的,但您可以指定任何类型)。

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).

可爱咩 2024-10-24 02:57:07

好的,这是一个完整的示例: http://nishi.dreamhosters.com/u/so_bmp_v0.zip< /a>

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文