Display 256 color bitmap in VC5
Li Xin Zhou Xuewei
In Windows programming, the display and processing of color images has always been an important topic, even in graphics cards.
Today, today, it is also meaningful to discuss the display of 256 color bitmaps. Through the discussion of this topic,
You can learn how to implement loading images, create and use palettes in VC5, and finalize the image.
In general, a 256-colored bitmap should be displayed first, and the information of the figure should be obtained, through the color of the bitmap.
Table Create a logical palette, then select this palette to the device environment, implement this palette, and finally
Bitmap is copied with a Bitblt function to the device environment.
The specific implementation steps are as follows:
1, load bitmap and create a palette.
First put into a bitmap, which can be tied together in the form of a resource, or file
Forms from outside. This bitmap is then contacted with a CBitmap object. Here I am here.
They should use the API function loadImage (), not the member function of the CBitmap class cBitmap :: loadbitmap (),
Because we need to get the DibSection structure of this bitmap, we can get the color of this bitmap from this structure.
Color information, thereby building a logical palette that matches these colors. Use cbitmap :: loadbitmap ()
The color information of the bitmap we need will be lost.
After getting a bitmap, the next step is to obtain the color information of this bitmap. Letter via cBitmap: getObject ()
Number, we can access the DibSECTION structure, get the number of colors of bitmaps. Generally speaking, these information
In the BitmapInfoHead structure, but as part of the Dibsection structure, BitmapInfohead
Sometimes, how many colors have been used not to indicate; encounter this situation, we can look at each pixel of the image.
Several (bits) to describe the color, if it is 8, because the 8-bit binary number can represent 256 different
Value, so the image is 256 colors; the sympathizes, 16bit indicates 64k colors. Got the number of colors used by the bitmap,
You can create a logical palette. The bitmap of more than 256 colors is no color table, at this time
We only use simple creation of a halftone palette (Halftone Palette, which is compatible with the device environment.
The samples of all different colors are included in the halftone palette. This is obviously not the best solution, but it is the simplest.
For bitmaps for the number of colors less than or equal to 256, we have to create a new palette from head. Allocate
Enough memory space to load the color table of the image, the color table can be obtained using the API function getDibcolortable;
Then allocate enough memory to the newly built logical palette, copy the color table information you have just copied to the new
The Palpalentry field in the palette and set the Palversion domain to 0x300. After creating a palette, the window should be
Infinished redrawing. In a specific implementation, we define a function getBitMapandPalette () to implement the bit.
The creation of the mount resource and the creation of the logical palette, its functional implementation block diagram (omitted)
The function is implemented as follows:
Bool getBitmapandpalette (LPCTSTR LPSZRESOURCE,
CPitmap & Bitmap, CPALETTE & PAL, Long * W, Long * H)
{
LPCTSTR LPSZRESOURCENAME = (lpctstr) lpszResource;
Hbitmap HBMP = (Hbitmap) :: loadImage (AfxgetInstanceHandle (), LPSZRESOURCENAME, Image_bitmap, 0, 0,
LR_loadfromfile; // Load bitmap
IF (HBMP == Null)
Return False;
Bitmap.attach (HBMP);
// Connect this bitmap with a CBITMAP object
Dibsection DS;
BitmapInfoheader & bminfo = ds.dsbmih;
Bitmap.GetObject (SizeOf (DS), & DS);
// Get the number of bitmap colors
INT ncolors = bminfo.Biclrused?
Bminfo.Biclrused: 1 << bminfo.bibit; * w = "bminfo.biwidth;" // Get bitmap width value * h = "bminfo.biheight;" // Get bitmap height value CclientDC DC (NULL); / / Create a logical palette if (ncolors> 256)
Pal.CreatehalftonePalette (& DC);
Else
{
// Color Number <= 256 RGBQUAD * prgb = "new" RGBQUAD [ncolors]; CDC memdc; memdc.CreateCompatibleDC (& dc); memdc.SelectObject (& bitmap); :: GetDIBColorTable (memdc, 0, ncolors, prgb); UINT NSIZE = "SIGPALETTTTTTTTTT" (SIGPALETRY) * NCOLORS; LogPalette * PLP = "(Logpalette" *) New byte [nsize]; plp-> pave = 0x300;
PLP-> PALNUMENTRIES = NCOLORS;
For (int i = 0; I palpalentry [i] .pered = prg [i] .rgbred;
PLP-> Palpalentry [i] .pegreen = prg [i] .rgbgreen;
PLP-> Palpalentry [i] .peblue = prg [i] .rgbblue;
PLP-> Palpalent [I] .peflags = 0;
}
PAL.CREATEPALETTTE (PLP);
DELETE [] PLP;
delete [] prgb;
}
Return True;
}
2. Display bitmap
The response function onpaint () is implemented in the WM_PAINT message.
void onpaint ()
{
CPAINTDC DC (this); // Device Context for Painting
// Create a Memory DC Compatible with the Paint DC
CDC MEMDC;
Memdc.createCompatibleDC (& DC);
CBitmap Bitmap;
CPALETTE PALETTE;
Long nwidth;
Long NHEIGHT;
// Call this function
GetBitmapandpalette ("E: // Project ////
ShowImage // bitmap1.bmp ", Bitmap, Palette,
& nwidth, & nHeiGHT);
MEMDC.SELECTOBJECT (& Bitmap); // Select and realize the Palette
IF (DC.GetDeviceCaps (RasterCaps) &
RC_PALETTE && PALETTE.M_HOBJECT! = NULL)
{
Dc.selectPalette (& Palette, False);
Dc.RealizePalette ();
}
// Display bitmap
Dc.bitblt (0, 0, NWIDTH, NHEIGHT, & MEMDC, 0, 0, SRCCOPY);
}
The above program is just simple from a fixed path (E: //project//showimage//bitmap1.bmp)
Load bitmap, readers can expand their function, such as passing the dialog box, etc., not described here.
The last thing to add is that if the bitmap to display is to contact the program as a bitmap resource,
The modification method is as follows:
First change the getBitMapandPalette () function to:
Bool getBitmapandpalette (uint nidresource,
CPitmap & Bitmap, CPALETTE & PAL, Long * W, Long * H)
Where NidResource is the ID of this bitmap.
Then change the first sentence in GetBitMapandPalette () to:
LPCTSTR LPSZRESOURCENAME = (LPCTSTR) NidResource;
And change the loadImage function to:
Hbitmap hbmp = (hbitmap) :: loadimage (AfxgetInstanceHandle (),
NidResourceName, Image_bitmap, 0, 0,
LR_createdibsection;
Finally, when you call getBitMapandPalette () in the onpaint function,
Put the ID of the bitmap
It can be incorporated by NidResource.