C + + read bitmap information display bitmap Bitmapinfoheader

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.