Original link: http://blog.csdn.net/chenlycly/article/details/9193143
To test the effect of CImage drawing a PNG image, we use the software to intercept a 360 interface, and then use tools such as Photoshop to add a transparent area around the image and then save it as a PNG image file. The CImage is first loaded from the file, i.e.
Cimage* M_PIMGBK;
......
M_PIMGBK = new CImage;
M_pimgbk->load (_t ("res\\bk.png"));
if (M_pimgbk->isnull ())//Picture loading failed
{
Delete M_PIMGBK;
M_PIMGBK = NULL;
}
And then to the OnPaint in the Test dialog box,
void Ctestcimagedrawdlg::onpaint ()
{
Cdialogex::onpaint ();
CWINDOWDC DC (this);
if (M_PIMGBK! = NULL)
{
M_pimgbk->draw (DC. GETSAFEHDC (), (+), M_pimgbk->getwidth (), M_pimgbk->getheight ());
}
}
As a result, some of the following problems have been found.
1, directly using CImage to draw a PNG image with transparent parts, transparent area does not penetrate (non-scaling)
As the original size of the picture is drawn to the test dialog interface, the transparent area is not erased, as shown in the code below.
CWINDOWDC DC (this);
if (M_PIMGBK! = NULL)
{
Imgbk->draw (DC. GETSAFEHDC (), (+), M_pimgbk->getwidth (), M_pimgbk->getheight ());
}
The display is shown below.
The PNG images with transparent areas are checked for additional processing to determine if alpha transparent channels are enabled and, if enabled, to be processed as follows:
if (m_pimgbk->getbpp () = = +)
{
for (int i = 0; i < m_pimgbk->getwidth (); i++)
{
For (int j = 0; J < M_pimgbk->getheight (); j + +)
{
unsigned char* puccolor = reinterpret_cast<unsigned char *> (m_pimgbk->getpixeladdress (i, J));
puccolor[0] = puccolor[0] * puccolor[3]/255;
puccolor[1] = puccolor[1] * puccolor[3]/255;
puccolor[2] = puccolor[2] * puccolor[3]/255;
}
}
}
the treated area is filtered out, as shown below.
2, using CImage::D raw directly draw scaled PNG picture, the display is not complete, distortion is serious
Considering that in some cases the PNG picture is scaled, the zoom drawing effect is tested. To zoom in and out of length and width, the relevant code is shown below.
CWINDOWDC DC (this);
if (M_PIMGBK! = NULL)
{
int ndstwidth = 450;
int ndstheight = (int) ((Ndstwidth*1.0/m_pimgbk->getwidth ()) *m_pimgbk->getheight ()); Width and high scale scaling
M_pimgbk->draw (DC. GETSAFEHDC (), Ndstwidth, ndstheight);
}
Check out MSDN to see if there are interfaces or parameters that can handle this scaling problem better. Found in CImage::D raw can add gdiplus::interpolationmode parameters, go used to look at a bit, you can choose gdiplus::interpolationmodehighquality high-quality type, It is found that the scaling distortion has improved a lot, but the transparent part that should have gone out is black, as shown below.
Therefore, the CImage handles PNG images with transparent portions, especially when zooming is defective. Later, instead of using the image class of GDI +, there is no similar problem. in fact, CImage internal is also using GDI + implementation, the specific reasons why the above problems are not clear. PNG images can be processed directly using the image class of GDI +. Using the image class is drawn with Gdiplus::graphics, which uses the image to load the picture, using Gdiplus::graphics to draw the image from the image to the interface DC, as shown in the code below.
Gdiplus::graphics Graphics (DC);
Graphics. DrawImage (M_pimage, Ndstwidth, Ndstheight)
Problems when you use the MFC CImage class to draw PNG pictures