VC + + Load Transparent PNG image method-gdi+ and CImage two kinds

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/zhongbin104/article/details/8730935

First look at the GDI + method
Method 1:

 1.gdi+ PNG image with transparent layer (alpha)

StdAfx join as follows:

 
#include//Initialize the COM port #include "GdiPlus.h" using namespace GdiPlus; #pragma comment (lib, "Gdiplus.lib")
 

Start initialization:
In the declaration of the App Class (. h), add:

 
ULONG_PTR M_gdiplustoken;
  

InitInstance () added://If there is no usingnamespace Gdiplus; Add Gdiplus in front::

 
Gdiplusstartupinput Gdiplusstartupinput; Gdiplusstartup (&m_gdiplustoken, &gdiplusstartupinput, NULL);
 

Heavy-duty ExitInstance, added Gdiplusshutdown (M_gdiplustoken);

 
int cxxxapp::exitinstance () {//TODO: Add private code here and/or call base class Gdiplusshutdown (M_gdiplustoken); return CWinApp::ExitInstance () ;}
 

The process of displaying the picture is as follows

CClientDC *PDC = new CClientDC (GetDlgItem (idc_static_pic)); CRect rect; GetDlgItem (idc_static_pic)->getwindowrect (&rect); Graphics graphics (PDC->M_HDC); Create a GDI + Graphics object image Image (_t ("1.png")); Construct an image graphics. DrawImage (&image, 0, 0, image. GetWidth (), image. GetHeight ()); Delete PDC;

This is to display the picture with GDI +.

 2.CImage draw a PNG image with alpha transparent layers MFC's own CImage can also be displayed, but a little conversion to get a normal PNG image with the alpha channel! convert one time before drawing, where image is an object of CImage
if (image.getbpp () = = 32)//Confirm that the image contains alpha channel {int i; Int J; for (i = 0; i < image.getwidth (); i++) {for (j = 0; J < i Mage. 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] * pbyte[3]/255;}}}

Here's how:

 
HWND hwnd = GetSafeHwnd (); Gets the HWND of the window:: InvalidateRect (hwnd, NULL, True); Or:: InvalidateRect (hwnd, NULL, FALSE); :: UpdateWindow (HWND); If you do not want to use the original drawing to remove the picture, you can delete the above three-segment CDC *PDC = GetDC (); CImage Image;image. Load (strpath);  if (Image.isnull ()) {MessageBox (_t ("Not loaded successfully")); return-1;} if (image.getbpp () = = 32)//Confirm that the image contains alpha channel {int i; Int J; for (i = 0; I < image.getwidth (); i++) {for (j = 0, J < Image.getheight (); j + +) {byte *pbyte = (BYTE *) image.getpixeladdress (i, J);p byte[0] = pbyte[0] * p BYTE[3]/255;pbyte[1] = pbyte[1] * pbyte[3]/255;pbyte[2] = pbyte[2] * pbyte[3]/255;}}} Image.draw (PDC->M_HDC, 0, 0); Image. Destroy (); ReleaseDC (PDC);
The internal framework of the code is the image of the re-processing, the original was modified, so that the more normal, the code is measured as followspost-Drawing effects 3. If the picture is in a resource, the loading method is different. This requires two functions,for GDI + as follows: 
BOOL Imagefromidresource (UINT nID, LPCTSTR str,image *&pimg) {hinstance hInst = AfxGetResourceHandle (); Hrsrc hrsrc =:: FindResource (Hinst,makeintresource (NID), STR); Type if (!HRSRC) return FALSE; Load resource into memory DWORD len = Sizeofresource (HInst, HRSRC); byte* lprsrc = (byte*) loadresource (HInst, HRSRC); if (!LPRSRC) return FALSE; Allocate global memory on which to create stream hglobal M_hmem = GlobalAlloc (gmem_fixed, Len); byte* Pmem = (byte*) globallock (M_HMEM); memcpy (Pmem,lprsrc,len); GlobalUnlock (M_HMEM); istream* pstm; CreateStreamOnHGlobal (m_hmem,false,&pstm); Load from Stream Pimg=gdiplus::image::fromstream (pstm); Free/release stuff Pstm->release (); FreeResource (LPRSRC); GlobalFree (M_HMEM); return TRUE;}
 the code that loads the picture then changes to:
CClientDC *PDC = new CClientDC (GetDlgItem (idc_static_pic)); CRect rect; GetDlgItem (idc_static_pic)->getwindowrect (&rect); Graphics graphics (PDC->M_HDC); Create a GDI + Graphics object Image *pimage; Construct an image Imagefromidresource (idb_png1,_t ("PNG"), Pimage), graphics. DrawImage (pimage, 0, 0,pimage->getwidth (), Pimage->getheight ()); Delete PDC;
    the following functions are required when using CImage :  
 bool Loadimagefromresource (CImage *pimage, UINT nresid,lpctstr Lptyp) {if (pImage = = NULL) return False;pimage->destroy (); Find resources Hrsrc HRSRC =:: FindResource (AfxGetResourceHandle (), Makeintresource (NRESID), Lptyp); if (hrsrc = = NULL) return false; Load Resources Hglobal Himgdata =:: LoadResource (AfxGetResourceHandle (), HRSRC); if (Himgdata = = NULL) {:: FreeResource (Himgdata); return false;}//Lock the specified resource in memory lpvoid LPVOID =:: Lockresource (Himgdata); Lpstream PStream = Null;dword dwsize =:: Sizeofresource (AfxGetResourceHandle (), HRSRC); Hglobal hnew =:: GlobalAlloc (GHND, dwsize); Lpbyte lpbyte = (LPBYTE):: GlobalLock (Hnew);:: memcpy (Lpbyte, LPVOID, dwsize); Unbind the specified resource in memory:: GlobalUnlock (Hnew); Creates a stream object from the specified memory HRESULT HT =:: CreateStreamOnHGlobal (Hnew, TRUE, &pstream); if (HT! = S_OK) {GlobalFree (hnew);} else {//Load Picture pimage->load (PStream); GlobalFree (hnew);} Release resources:: FreeResource (Himgdata); return true;}  
 the code to load the picture is as follows:
HWND hwnd = GetSafeHwnd (); Gets the HWND of the window:: InvalidateRect (hwnd, NULL, True); Or:: InvalidateRect (hwnd, NULL, FALSE); :: UpdateWindow (HWND); CDC *PDC = GetDC (); CImage Image; Loadimagefromresource (&image,idb_png1,_t ("PNG"));  if (Image.isnull ()) {MessageBox (_t ("Not loaded successfully")); return;} if (image.getbpp () = = 32)//Confirm that the image contains alpha channel {int i; Int J; for (i=0; I<image.getwidth (); i++) = "" "{=" "<span=" ">for (j=0; J<image.getheight (); j + +) =" "" {= "" byte= "" *pbyte= "(byte" *) Image.getpixeladdress (i,= "" j); = "" pbyte[<span= "" >0] = pbyte[0] * pbyte[3]/255;pbyte[1] = pbyte[1] * PByte[3]/25 5;PBYTE[2] = pbyte[2] * pbyte[3]/255;}}} Image.draw (pdc->m_hdc,0,0); Image. Destroy (); ReleaseDC (PDC);
  otherNote that it is best to put the drawing in the OnPaint message response, otherwise, the ONSIZE message triggers OnPaint may erase all previous drawings when redrawn



VC + + Load Transparent PNG image method-gdi+ and CImage two kinds

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.