Draw BMP bitmap file

xiaoxiao2021-03-06  87

Draw BMP bitmap file

The BMP file consists of three parts: BitmapfileHeader for file information, related bitmap

Information of BitmapInfo and bitmap image data. Among them, BitmapInfo is also from bitmap information headers.

BitmapInfoHeader and color table consists, color table is not

necessary.

A definition of several structures can view online help. Once the BMP bitmap is read into memory, as long as the last

Display bitmap

The method can be displayed.

Function 1: Load bitmap file, generate bitmap GDI objects, display with DDB methods.

// loadbmpimage - Load BMP bitmap, create bitmap GDI objects, create bitmap tone

// Returns - Return true success

// sbmpfile - full circuit BMP file

// bitmap - bitmap object that will be initialized

// ppal - The bitmap palette that will be initialized can be null

Bool LoadBmpimage (LPCTSTSTSTSTSTSBMPFILE, CBITMAP & BITMAP, CPALETTE * PPAL)

{

CFILE FILE;

IF (! file.open (sbmpfile, cfile :: moderad))

Return False;

BitmapfileHeader Bmfheader;

// read the file header

IF (file.read ((lpstr) & bmfheader, sizeof (bmfheader))! = sizeof (bmfheader))

Return False;

// File type flag is 'bm'

IF (BMFHeader.BfType! = ((Word) ('M' << 8) | 'B'))

Return False;

// Number of memory required to obtain files

DWord npackedDiblen = file.getlength () - sizeof (bitmapfilehead);

Hglobal HDIB = :: GLOBALLOC (GMEM_FIXED, NPACKEDDIBLEN);

IF (HDIB == 0)

Return False;

// Read the bits of bitmap file

IF (file.readhuge ((lpstr) HDIB, NPACKEDDIBLEN)! = npackedDiblen

{

:: GlobalFree (HDIB);

Return False;

}

BitMapInfoHeader & bmiheader = * (lpbitmapinfohead) HDIB;

BitmapInfo & bminfo = * (lpbitmapinfo) HDIB;

// bitmap color

INT ncolors = bmiheader.birused? bmiheader.birrused:

1 << bmiheader.biBitcount;

LPVOID LPDIBBCI;

IF (bminfo.bmiheader.bibitcount> 8)

LPDIBBITS = (LPDWORD) (Bminfo.bmicolors Bminfo.Bmiheader.Biclrused)

((Bminfo.Bmiheader.Bicompression == bi_bitfields)? 3: 0));

Else

LPDIBBITS = (LPVOID) (Bminfo.bmicolors Ncolors);

// Create a logical palette

IF (ppal! = null)

{

IF (ncolors <= 256)

{

Uint nsize = sizeof (sizeof (paletteentry) * ncolors; logpalette * plp = (logpalette *) new byte [nsize];

PLP-> paversion = 0x300;

PLP-> PALNUMENTRIES = NCOLORS;

For (INT i = 0; I

{

PLP-> palpalentry [i] .pled = bminfo.bmicolors [i] .rgbred;

PLP-> Palpalent [i] .pegreen = bminfo.bmicolors [i] .rgbgreen

PLP-> Palpalentry [i] .peblue = bminfo.bmicolors [i] .rgbblue;

PLP-> Palpalent [I] .peflags = 0;

}

PPAL-> CREATEPALETTE (PLP);

DELETE [] PLP;

}

}

CClientDC DC (NULL);

CPALETTE * POLDPALETTE = NULL;

IF (ppal)

{

Poldpalette = dc.selectpaalette (ppal, false);

Dc.RealizePalette ();

}

Hbitmap hbmp = createdibitmap (dc.m_hdc, // device handle

& bmiheader, // Pointer to Bitmap Size and Format Data

CBM_INIT, // Initialization Flag

LPDIBBITS, / / ​​POINTER TO Initialization Data

& bminfo, // Pointer to Bitmap Color-Format Data

DIB_RGB_COLORS); // Color-Data Usage

Bitmap.attach (HBMP);

IF (Poldpalette)

Dc.selectPalette (Poldpalette, False);

:: GlobalFree (HDIB);

Return True;

}

Function 2: Load bitmap file, generate bitmap device handles, display with DIB methods.

// loadbmp - Load bitmap file, get bitmaphe and bitmap palette

// Returns - Return true success

// SBMPFILE - full path BMP file

// PhDIB - pointing to a hglobal handle

// ppal - pointing to the bitmap palette

Bool LoadBMP (LPCTSTSTSTSTSBMPFILE, HGLOBAL * PPAL)

{

CFILE FILE;

IF (! file.open (sbmpfile, cfile :: moderad))

Return False;

BitmapfileHeader Bmfheader;

Long nfilelen;

Nfilelen = file.getlength ();

// read file header

IF (file.read ((lpstr) & bmfheader, sizeof (bmfheader))! = sizeof (bmfheader))

Return False;

// File Type Should Be 'BM'

IF (BMFHeader.BfType! = ((Word) ('M' << 8) | 'B'))

Return False; Hglobal HDIB = :: GMEM_FIXED, NFILEN;

IF (HDIB == 0)

Return False;

// read the remainder of the bitmap file.

IF (file.readhuge ((lpstr) hdib, nfilelen - sizeof (bitmapfileHeader)! =

Nfilelen - sizeof (BitmapfileHeader)

{

:: GlobalFree (HDIB);

Return False;

}

BitmapInfo & bminfo = * (lpbitmapinfo) HDIB;

Int ncolors = bminfo.bmiheader.biclrused? bminfo.bmiheader.biclrused:

1 << bminfo.bmiheader.biBitcount;

// Create the Palette

IF (ncolors <= 256)

{

UINT nsize = sizeof (logpalette) (SizeOf (Paletteentry) * Ncolors;

Logpalette * PLP = (logpalette *) New byte [nsize];

PLP-> paversion = 0x300;

PLP-> PALNUMENTRIES = NCOLORS;

For (INT i = 0; I

{

PLP-> palpalentry [i] .pled = bminfo.bmicolors [i] .rgbred;

PLP-> Palpalent [i] .pegreen = bminfo.bmicolors [i] .rgbgreen

PLP-> Palpalentry [i] .peblue = bminfo.bmicolors [i] .rgbblue;

PLP-> Palpalent [I] .peflags = 0;

}

PPAL-> CREATEPALETTE (PLP);

DELETE [] PLP;

}

* phdib = HDIB;

Return True;

}

转载请注明原文地址:https://www.9cbs.com/read-122177.html

New Post(0)