Display a 256-color bitmap in vc5

Source: Internet
Author: User
 

Display a 256-color bitmap in vc5

Li Xin Zhou xueyong

---- In Windows programming, the display and processing of color images has always been an important topic. Even if the video card supports true color, it is meaningful to discuss the display of 256 color bitmap. Through the discussion of this topic, you can understand how to mount images in vc5, create and use the color palette, and finally draw images.

---- In general, to display a 256-color bitmap, you should first obtain the information about the map, create a logical palette through the bitmap's color table, and then select the palette into the device environment, implement this palette, and finally copy the bitmap to the device environment using the bitblt function.

---- The specific steps are as follows:

---- 1. Load the bitmap and create a color palette.

---- First, load a bitmap, which can be bound together with a program in the form of resources or mounted from the outside as a file. Then, associate the bitmap with a cbitmap object (attach. Here we should use the API function LoadImage () instead of the cbitmap class member function cbitmap: loadbitmap (), because we need to obtain the dibsection structure of the bitmap, from this structure, we can obtain the color information of the bitmap to create a logical palette that matches these colors. Using cbitmap: loadbitmap () will lose the color information of the bitmap we need.

---- After obtaining the bitmap, the next step is to obtain the color information of the bitmap. Through the cbitmap: GetObject () function, we can access the dibsection structure to obtain the color number of Bitmap. Generally, this information exists in the bitmapinfohead structure. However, as part of the dibsection structure, bitmapinfohead sometimes does not indicate how many colors the image uses, we can see that each pixel of an image uses several bits to describe the color. If it is 8 bits, the 8 bits binary number can represent 256 different values, therefore, the image is 256 colors. Similarly, 16 bits indicate 64 K colors. After obtaining the number of colors used by the bitmap, you can create a logical color palette. The bitmap with more than 256 colors does not have a color table. In this case, we only need to create a halftone palette that is compatible with the device environment, the halftone palette contains samples of all different colors. This is obviously not the best solution, but it is the simplest.

---- For bitmaps with less than or equal to 256 colors, we need to create a new color palette from the beginning. Allocate enough memory space to load the image's color table. The color table can be obtained using the API function getdibcolortable. then allocate enough memory to the new logical color palette, copy the obtained color table information to the palpalentry field in the new color palette, and set the palversion field to 0x300. After creating a color palette, refresh the window and re-draw it. In specific implementation, we define a function getbitmapandpalette () to load bitmap resources and create a logical palette. The function implementation diagram is as follows (omitted)

---- Function implementation:

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); // loads a bitmap.

If (hbmp = NULL)
Return false;

Bitmap. Attach (hbmp );
// Associate the bitmap with a cbitmap object

Dibsection Ds;
Bitmapinfoheader & bminfo = Ds. dsbmih;
Bitmap. GetObject (sizeof (DS), & Ds );

// Obtain the number of Bitmap colors.
Int ncolors = bminfo. biclrused?
Bminfo. biclrused: 1 <bminfo. bibitcount; * w = "bminfo. biwidth; "// obtain the bitmap width value * H =" bminfo. biheight; "// obtain the bitmap height value cclientdc DC (null); // create a logical color palette if (ncolors> 256)
Pal. createhalftonepalette (& DC );
Else
{
// Number of colors <= 256 rgbquad * prgb = "new" rgbquad [ncolors]; CDC memdc; memdc. createcompatibledc (& DC); memdc. selectObject (& Bitmap);: getdibcolortable (memdc, 0, ncolors, prgb); uint nsize = "sizeof (logpalette)" + (sizeof (paletteentry) * ncolors ); logpalette * PLP = "(logpalette" *) New byte [nsize]; PLP-> palversion = 0x300;
PLP-> palnumentries = ncolors;

For (INT I = 0; I Palpalentry [I]. pered = prgb [I]. rgbred;
PLP-> palpalentry [I]. pegreen = prgb [I]. rgbgreen;
PLP-> palpalentry [I]. peblue = prgb [I]. rgbblue;
PLP-> palpalentry [I]. peflags = 0;
}

Pal. createpalette (PLP );

Delete [] PLP;
Delete [] prgb;
}

Return true;
}

2. Display bitmap
It is implemented in onpaint (), the response function of 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 simply loads a bitmap from a fixed path (E: // project // showimage // bitmap1.bmp). You can expand its functions, such as selecting through the dialog box, I will not repeat it here.

---- The last thing to add is that if the bitmap to be displayed is associated with the program as a bitmap resource, the above program can be displayed after slight modification. The modification method is as follows:

First, change the getbitmapandpalette () function to bool getbitmapandpalette (uint nidresource, cbitmap & bitmap, cpalette & pal, long * w, long * H), where nidresource is the ID of the bitmap. Then, change the first sentence in getbitmapandpalette () to "maid" and change the LoadImage function to "hbitmap hbmp = (hbitmap): LoadImage (Inline (), nidcenresourame, image_bitmap, lr_createdibsection); finally, when the onpaint function calls getbitmapandpalette (), pass the bitmap ID through nidresource.

 

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.