Display 256 color bitmap in VC5 ---- In Windows programming, the display and processing of color images has always been an important topic, even in the graphics card generally supports true color today, discuss 256 color bitmap It is also meaningful. By discussing this topic, you can learn how to implement loading images, create and use palette in VC5, and finalize images. ---- In general, a 256-color bitmap should be displayed first, and the information should be obtained, create a logical palette through the color table of the bitmap, then select this palette to the device Environment, implement this palette, and finally copy the bitmap to the Bitblt function to the device environment. ---- Specific implementation steps are as follows: ---- 1, load bitmap and create a palette. ---- First loading a bitmap, which can be tied together in the form of a resource, or in the form of a file from the outside. This bitmap is then contacted with a CBitmap object. Here we should use the API function loadImage (), not the member function cBitmap :: loadbitmap () of the CBitmap class, because we need to get the DibSECTION structure of this bitmap, from this structure we can get the color information of this bitmap, Thereby a logical palette that matches these colors. Use cbitmap :: loadbitmap () will lose color information of the bitmap we need. ---- After getting a bitmap, the next step is to obtain the color information of this bitmap. With the cBitmap: getObject () function, we can access the DibSECTION structure, which get the number of colors of bitmaps. In general, this information is present in the BitmapInfohead structure, but as part of the Dibsection structure, BitmapInfohead sometimes does not indicate how many colors have been used; encounter this situation, we can look at each pixel of the image. Several (bit) to describe colors, if it is 8 bits, because the 8-bit binary number can represent 256 different values, the image is 256 colors; the sympathy, 16bit indicates that is 64K color. You can create a logical palette if you get the number of colors used in your bitmap. The bitmap of more than 256 colors is there is no color table. At this time, we only create a halftone palette (HALFTONE PALETTE) that is compatible with the device environment, in the halftone palette Contains all different colors samples. This is obviously not the best solution, but it is the simplest. ---- For bitmaps with less than or equal to 256, we have to create a new palette from head. First allocate enough memory space to load the color table of the image, the color table can be obtained using the API function getDibcolortable; then assign enough memory to the newly built logical palette, copy the color table information you just get to new color toning The Palpalentry field in the board and set the Palversion domain to 0x300. After creating a palette, you should refresh the window.
In a specific implementation, we define a function getBitMapandPalette () to implement the creation of bitmap resources, and the functional implementation block diagram is as follows (omitted) ---- function is implemented as follows: BOOL getBitMapandpalette LPCTSTR lpszresource, CBitmap & 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); // Link the bitmap with a cbitmap object Dibsection DS; BitmapInfoHeader & Bminfo = DS.DSBMIH; Bitmap.GetObject SizeOf (DS), & DS); // Get bitmap color Int ncolors = bminfo.biBLRUSED? Bminfo.BiBitCount: 1 << bminfo.bibitcount; * w = "bminfo.biwidth;" // Get bitmap width value * H = "bminfo.biheight;" gets the 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 = "sizeof (LOGPALETTE) (Si) Zeof (Paletteentry) * ncolors; logpalette * plp = "(logpalette" *) new byte [nsize]; plp-> paRversion = 0x300; PLP-> PALNUMENTRIES = NCOLORS; for (int i = 0; ipalpalent [i]. Pred = 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 DCCDC 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 procedure is just simple from a fixed path (e: //project/showimage//bitmap1.bmp) to the bitmap, the reader can expand its function, such as selecting the dialog box, etc., here Not to repeat it. ---- The last thing to add is that if the bitmap to be displayed is to contact the program as a bitmap resource, you can display it slightly, the modification method is as follows: First, getBitMapandPalette () The function is changed 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.