Array[Byte] to Hbitmap or Cbitmap

array[byte] to HBITMAP or CBitmap

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.

How do I get the actual byte array from a Windows LPBITMAP or HBITMAP?

If you have a LPBITMAP then the bits are already there, in BITMAP::bmBits.
If you have a HBITMAP you can use GetDIBits to get them.

CBitmap from raw RGB values in std::vectorstd::uint32_t holding array

CBitmap Chb;
HBITMAP bmp = CreateBitmap(width, height, 1, 32, &*img.begin());
ASSERT_ALWAYS(bmp != NULL)
Chb.Attach(bmp);
//PicControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE);
//PicControl.SetBitmap(Chb);
mProjectorWindow.m_picControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE);
mProjectorWindow.m_picControl.SetBitmap(Chb);

Load image from binary in MFC

Here's the solution I found out ,and it works fine in my project.

1.function for retrieve binary data from sqlite database.

int CSqlite::retrieveBinaryData(int id , byte *pdata)
{
ASSERT(pdata != NULL);
int rc,size;
sqlite3_stmt * stmt;

sqlite3_prepare(dbh,"select id,content,size from images where id = ?",-1,&stmt,0);

sqlite3_bind_int(stmt,1,id);
rc = sqlite3_step(stmt);

if(rc == SQLITE_ROW)
{
num = sqlite3_column_int(stmt,0);

size = sqlite3_column_int(stmt,2);
byte *tmpdata = (byte * )sqlite3_column_blob(stmt,1);
memcpy(pdata,tmpdata,size);
return size;
}
return 0;
}

2.receive the data from function above and convert it to CBitmap so we can use it.

    byte *tdata = new BYTE[BMP_MAX_SIZE];
CSqlite *sq = new CSqlite("mysqlite.db");
int size = sq->retrieveBinaryDatas(myid,tdata);

/* using the resource mentioned by alfonso in the comment */

BITMAPFILEHEADER* bmfh;
bmfh = (BITMAPFILEHEADER*)tdata;

BITMAPINFOHEADER* bmih;
bmih = (BITMAPINFOHEADER*)(tdata + sizeof(BITMAPFILEHEADER));
BITMAPINFO* bmi;
bmi = (BITMAPINFO*)bmih;

void* bits;
bits = (void*)(tdata + bmfh->bfOffBits);

HDC hdc = ::GetDC(NULL);

HBITMAP hbmp = CreateDIBitmap(hdc, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS) ;

::ReleaseDC(NULL, hdc);
delete tdata;

BITMAP bitmap;
CBitmap *bmpBackground = CBitmap::FromHandle(hbmp);

How to convert Gdiplus::Bitmap* object into HBITMAP

Not a direct answer but a way to load a stream into a bitmap. It doesn't need GDI+ at all.

  1. Use CImage::Load
  2. Than use the HBITMAP conversion operator

CImage is a direct slim wrapper for a HBITMAP object from the ATL.



Related Topics



Leave a reply



Submit