MFC loads PNG images and implements double buffering

Source: Internet
Author: User

Because PNG contains an alpha channel, unlike bitmap, it is handled in MFC using the CImage class, typically using the load and draw member functions.

So the title of the discussion can be further explained as, using CImage to achieve double buffering.

The usual double-buffering method is (the function of the message function afx_msg BOOL onerasebkgnd (cdc* PDC) is modified to return TRUE first):

CDC MEMDC; CBitmap bmp;bmp. CreateCompatibleBitmap (pdc,window_width,window_height); Memdc.createcompatibledc (PDC); MemDC.SelectObject (& BMP);//drawing code BMP. DeleteObject (); Memdc.deletedc ();

CImage can load the picture through the load member function and draw the picture onto the DC via the draw member function.

The draw member function is prototyped as a bool Draw (HDC hdestdc, ...), which also needs to get the specified DC, but flashes when a picture is drawn to the DC on one sheet.

Because the idea of double buffering is--draw the diagram first, then copy the past uniformly.

So, the solution is to stick all the pictures on the same CImage object, then unify and then call the draw member function to draw to the DC (if there are other drawing operations in addition to the picture, the DC should be MEMDC). The following function is to draw all pictures first to one picture:

void Crepairclientdlg::showimg (CImage &image, int x, int y) {image. Draw (M_CSCREEN.GETDC (), x, y),//M_csreen for a CImage object M_cscreen.releasedc ();}

Another: the function of loading transparent pictures is as follows:

void Crepairclientdlg::loadimg (CImage &image, CString &strpath) {image. Load (strpath), if (image. IsNull ()) {MessageBox (_t ("Picture not loaded successfully!")); return;} Determine if transparent display is required and do the appropriate processing if (image. GETBPP () = = +) {for (int i = 0; i < image. GetWidth (); ++i) {for (int j = 0; J < image. GetHeight (); ++j) {byte *pbyte = (BYTE *) image. Getpixeladdress (i, J);p byte[0] = pbyte[0] * pbyte[3]/255;pbyte[1] = pbyte[1] * pbyte[3]/255;pbyte[2] = pbyte[2] * PBYT E[3]/255;}}}

  

MFC loads PNG images and implements double buffering

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.