Draw an irregular bitmap method summary, a variety of implementation methods, comprehensive test comparison!

zhaozj2021-02-12  150

The first thing to say is, the so-called allegorial bitmap drawing, meaning that a bitmap (bittap is always rule), there is an irregular graphics above, when this bitmap is drawn to a interface, The irregular graphics on the bitmap are required, and the background is kept in the background. For example, draw a sun, you can't cover the background of the rectangles in the sun, and you should only cover the sun, this article will fully introduce the drawing method, and their advantages and disadvantages! (I copied from my program and change the variable to a general name, where there may be a hand error) I said that these methods are in the VC, and the VB should be convenient in other formats such as GIF. However, it can be transparent, so this article is not targeting VB readers. These problems are all my problems in 9CBS, I hope to give my friends who have the same confused friends! W and h in the program are the width and height of the bitmap.

Method 1: First, the place other than irregular graphics (ie, the required transparent place), make the color (with image processing software) in the graphics, such as white, then use the following program: // Package letter # Include "Wingdi.h" / / Add: msimg32.lib in the object / library module in Linked in the project settings: msimg32.lib

CBITMAP YourBMP; YourBmp.LoadBitmap (IDB_XXXX); // To display bitmap

CDC * PDC = Getdc ();

CDC YOURDC; YourDc.createCompatibleDC (PDC); YourDc.SelectObject (& YourBMP);

TransparentBLT (PDC-> M_HDC, 0, 0, W, H, YourDc.m_HDC, 0, 0, W, H, RGB (255, 255, 255) // The RGB value of the transparent color in the bitmap);

ReleaseDC (PDC);

Evaluation: The program is simple, but the running speed is slow, there is a flashing (with a 208 * 15 bitmap test), so don't be convenient!

Method 2: Do a mask bitmap, the size is the same as the bitmap to be drawn, the resolution is the same, so that the mask corresponds to the graphic area is pure white, and the remaining places (the transparent place) is pure black.

CBITMAP YourBMP; YourBmp.LoadBitmap (IDB_XXXX); // To display bitmap

CBITMAP YOURMASKER; YOURMASKER.LOADBITMAP (IDB_XXXX); // Mandi Bitmap

CBitMap Background; Background.LoadBitmap (IDB_XXXX); / / Background Bitmap

CDC * PDC = Getdc ();

CDC YOURDC; YourDc.createCompatibleDC (PDC); YourDc.SelectObject (& YourBMP);

CDC Maskerdc; Maskerdc.createCompatibleDC (PDC); Maskerdc.selectObject (& YourMasker);

CDC Backgrounddc; Backgrounddc.createCompatibleDC (PDC); BackgroundDc.SelectObject (& Background);

CBITMAP TEMPBITMAP; // Temporary Bitchart Tempbitmap.createCompATIBLEBITMAP (PDC, W, H);

CDC Tempdc; Tempdc.createCompaPcript (PDC);

Tempdc.selectObject (& Tempbitmap); Tempdc.bitblt (0, 0, W, H, PDC, 0, 0, Srccopy); Tempdc.bitblt (0, 0, W, H, & YourDC, 0, 0, Srcand); Tempdc .Bitblt (0, 0, W, H, & Maskerdc, 0, 0, srcpaint); Tempdc.bitblt (0, 0, W, H, & YourDC, 0, 0, Srcand); PDC-> Bitblt (0, 0, W, h, & tempdc, 0,0, srccopy;

The top 5 behavioral row, the basic algorithm is to draw a bitmap in the temporary TEMPDC (the transparent area has been manifested), and then once (last line) draws to the screen, which prevents flashing, but to account for the CPU resources and Memory, tested on my 2.4G CPU, the CPU share is less than 1% (running a program every 100 milliseconds), acceptable.

ReleaseDC (PDC);

Evaluation: The program is complex, but the visual effect is better!

Method 3: Do a mask bitmap, the size is the same as the bitmap to be drawn, the resolution is the same, so that the mask corresponds to the graphic area is pure white, and the remaining places (the transparent place) is pure black.

CBITMAP YourBMP; YourBmp.LoadBitmap (IDB_XXXX); // To display bitmap

CBITMAP YOURMASKER; YOURMASKER.LOADBITMAP (IDB_XXXX); // Mandi Bitmap

CDC * PDC = Getdc ();

CDC YOURDC; YourDc.createCompatibleDC (PDC); YourDc.SelectObject (& YourBMP);

CDC Maskerdc; Maskerdc.createCompatibleDC (PDC); Maskerdc.selectObject (& YourMasker);

PDC-> Bitblt (0, 0, W, H, & YourDC, 0, 0, Srcand); PDC-> Bitblt (0, 0, W, H, & MaskerDC, 0, 0, SrcPaint); PDC-> Bitblt (0 , 0, W, H, & youredc, 0, 0, srcand;

ReleaseDC (PDC);

Evaluation: This method and method two essentially, the method two first stores the last one-time display, the method is three steps to display the bitmap, so the image drawn on the screen can change three times to achieve transparent The effect, so it is still slightly flashing, but it is much better than the method. It is less than the consumption of resources. It is in a compromise! It is recommended to use it very well in efficiency and visual, and WINDOW uses this method when moving the mouse. There is no reason to use this reason to use it more than this. After all, everyone is using His operating system!

Method 4: I have not passed the experiment because I didn't find the ICO production tool. The method is to make the bitmap you want to display into ICO. Custom ICO should be customized, not sure, everyone knows that ICO can be transparent, I want to transparency, then draw it on the screen on the Drawicon or DrawImage function, if this method is feasible, it is the simplest, as long as a line of programs!

Method 5: Use the CreateHatchBrush function to make a brush to display, and then make a zone contour to be displayed into a zone (to do the area, you can make it easy according to the color on the bitmap, the premise is to first The transparent place makes the color not appearing in the bitmap), and then use the FillRgn function to brush this area with the brush made, and finally achieve the effect. The method should require the area to be continuous, and the first point in the upper left corner cannot be transparent, so there should be no practical value! But I want to use the Bitblt function to improve this situation. Method 6: Use the alphablend function, the following procedure can only pass under VC . Net, because the concept of the transparent bitmap is used, and the 2000 system is not understood by the transparent bitmap.

// Brand letter #include "wingdi.h"

CBITMAP YourBMP; YourBmp.LoadBitmap (IDB_XXXX); // To display bitmap

CDC * PDC = Getdc ();

CDC YOURDC; YourDc.createCompatibleDC (PDC); YourDc.SelectObject (& YourBMP);

BlendFunction B; B.blendop = ac_src_over; // Default B.blendFlags = 0; // Default B.SourceConstantalpha = 255; // Do not have the overall transparency, with each quadprint 自己 自己 自己 自己 B.alphaformat = AC_SRC_AlphaFormat = ac_src_alphafha ;

There is no definition of AC_SRC_Alpha in //vc 6.0, only the definition of ac_src_no_alpha, which is the reason that cannot be used in VC 6.0.

Alphablend (PDC-> M_HDC, 0, 0, W, H, Gatedc.m_HDC, 0, 0, W, H, B);

ReleaseDC (PDC);

Evaluation: Method is simple, but requires more than 2000 system, and must be a 32-bit bitmap in advance and give the PLPHA to give the appropriate value (hereinafter discussed), I have not found a software that do 32-bit maps yet, Only the program is implemented, so I have implemented such a function!

Method for doing 32-bit transparent bitmap! This function can only turn 24 bits to 32 bits. If you can change it, you can convert other bitmaps. Before calling the function, please make the color you need to appear in the bitmap, assume it white . Note: This function is written to me, no error judgment, suppose I will not make mistakes, if you want to be more perfect, you can add the wrong capture.

BitmapfileHeader BF; BitmapInfoHeader Bi;

Cfiledialog DLG

(True, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "BMP file (* .bmp) | * .bmp ||"); cstring filename; cfile file;

IF (IDOK == DLG.Domodal ()) {filename = DLG.GETPATHNAME (); file.open (filename, cfile :: moderad | cfile :: sharednynone); // Open bitmap file file to be converted File.Read & bf, sizeof (bf)); File.read (& Bi, SIZEOF (BI));} /// updatedata (); // Get input in the Edit box, the Edit box is connected to the cstring variable Name, the input is used as a conversion Output file file name cfile output; output.open (name, cfile :: modecreate | cfile :: Mode, null); dword filteresize = bi.bisizeImage; // Data size Database data size DWORD simageSize = bi.biwidth * Bi.biheight * 4; // The image data size after conversion

INT SROWBYTE = bi.biwidth * 3; if (slowbyte% 4) SROWBYTE = 4-SROWBYTE% 4; // The number of bytes required for the previous line image before the conversion, pay attention to the multiple of 4, simply, the same height , 208 width and 207 wide 24-bit image of the file size is equal. // can also be referred to: SROWBYTE = bi.bisizeizeImage / bi.biheight; // Note that when the number of bytes of the 32-bit bit map is not judged to be a multiple of 4, and directly write into bi.biwidth * 4 It is very simple, and the 32-bit bitmap is //4 per pixel, so the number of bytes occupied by a line will be a multiple of 4.

Bf.bfsize = bf.bfoffbits simagesize;

Output.write (& BF, SIZEOF (BF)); // Write information to the output file

Bi.bisizeImage = simratesize; bi.biBITCOUNT = 32;

Output.write (& BI, SIZEOF (BI)); // Write information to the output file

bi.bisizeImage = fimagesize; // Restore previous BI data values ​​bi.biBitcount = 24; // Restore previous BI data values

/// char * sbuff = new char [SROWBYTE]; // Used to save source bitmap line information char * dbuff = new char [bi.biwidth * 4]; // Used to save the information of bitmap line

For (int J = 0; j

AfxMessageBox ("Transformation is complete!");

32-bit bitmap made in this function, the alphablend function is fully understood and running is normal!

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

New Post(0)