In C + +, to read a bitmap information bitmapinfoheader and display it on a control, the steps are as follows:
1. Read the bitmap information first
int readpicturebmp (unsigned char *pbmpbuffer)
{
//static int i=0;
//if (i>5)
//{
//return TRUE;
//}
FILE *FP = NULL;
Char Szfilename[max_path] = {0};
sprintf (szFileName, "e:\\ working directory \\20141008_162621_28826078.bmp");
fp = fopen (szFileName, "RB");
if (fp = = NULL)
{
return FALSE;
}
Long lsize;
/* Get File Size */
Fseek (FP, 0, Seek_end);
lsize = Ftell (FP);
Rewind (FP);
// Pbmpbuffer = new unsigned char[lsize];
Long res = fread (PBMPBUFFER,1,LSIZE,FP);
if (res! = lsize)
{
printf_s ("res \ n");
}
Fclose (FP);
fp = NULL;
return TRUE;
}
2. Preparing to display bitmaps
BOOL Preparedibbuffer (Lpbitmapinfo *LPLPBI, DWORD DwX, DWORD DwY)
{
HDC hDC
LpbitmapinfoheaderLpbih;
Allocate Buffer
*LPLPBI = new Bitmapinfo;
Lpbih = (lpbitmapinfoheader) *lplpbi;
HDC =:: GetDC (NULL);
Fill Information Bitmapinfoheader
lpbih->bisize = sizeof (Bitmapinfoheader);
Lpbih->biwidth = DwX;
Lpbih->biheight = DwY;
Lpbih->biplanes = 1;
Lpbih->bibitcount = 24;
Lpbih->bicompression = Bi_rgb;
lpbih->bisizeimage = 0;
Lpbih->bixpelspermeter = (GetDeviceCaps (hdc, horzres) * +)/GetDeviceCaps (hdc, horzsize);
Lpbih->biypelspermeter = (GetDeviceCaps (hdc, vertres) * +)/GetDeviceCaps (hdc, vertsize);
lpbih->biclrused = 0;
lpbih->biclrimportant = 0;
lpbih->bisizeimage = 0;
:: ReleaseDC (NULL, HDC);
Return TRUE;
}
3. Calling functions to display bitmaps
void Showbmp (HWND hwnd)
{
unsigned char *ppicbmpbuf;
ppicbmpbuf = new unsigned char[1024*1024*2];
Rect rect;
:: GetWindowRect (hwnd, &rect);
DWORD dwwidth = Rect.right-rect.left;
DWORD dwheight = rect.bottom-rect.top;
Readpicturebmp (PPICBMPBUF);
HDC HDRAWDC;
HDRAWDC =:: GetDC (HWND);
:: Setstretchbltmode (HDRAWDC, Coloroncolor);//delete pixel lines in device
bitmapinfo* Pbmi;
Preparedibbuffer (&pbmi, Nimgwidth, nimgheight);
Upper left corner stretchdibits This function allows JPEG or PNG images to be passed in as source images, it is important to remove the bitmap information from the head sizeof (Bitmapfileheader) +sizeof (bitmapinfoheader), otherwise
//display bitmap problems
:: StretchDIBits ( HDRAWDC, 0, 0, dwwidth, dwheight, \
0, 0, Pbmi->bmiheader.biwidth, pbmi- >bmiheader.biheight, \
(const void*) (Ppicbmpbuf+sizeof ( Bitmapfileheader) +sizeof (bitmapinfoheader)), \
Pbmi, Dib_rgb_colors, SRCCOPY); &NBSP
:: ReleaseDC (hwnd, HDRAWDC);
if (PPICBMPBUF)
{
delete []ppicbmpbuf;
Ppicbmpbuf = NULL;
}
if (PBMI)
{
delete []pbmi;
Pbmi=null;
}
}
C + + read bitmap information display bitmap Bitmapinfoheader