Click here to Skip to main content
15,114,004 members
Articles / Programming Languages / ASM
Posted 15 Jul 2007


61 bookmarked

A simple method to display a transparent PNG file

Rate me:
Please Sign up or sign in to vote.
3.65/5 (29 votes)
8 Sep 2008CPOL1 min read
Introducing a simple method to display a transparent PNG file.

Screenshot - snapshot.gif


If you are searching for a component to display a PNG32 image, this code may give you some tips.


Some days ago, I needed a component to display a transparent PNG image in my code. Searching with the keyword "PNG" in CodeProject, I found a demo named PNGView. But, it does not deal with transparent PNGs. So I decided to do it on my own.

The basic idea is to simulate AlplaBlend's behavior, see the following code. In order to fasten the speed of display, the code's core part is optimized by using ASM. I am not very good at ASM, so if there are any shortcomings or bugs in my code, please let me know.

Hope this helps. Thanks for your attention!

Using the code

I have wrapped the code to a static library. The download includes a demo and the static library's source code.

In order to use this code, you should import the lib first, and then you can display a PNG32 image in just three steps.

Step 1: Load a PNG32 image

In order to simplify the code, I only support 32 pixel depth PNG images.

char szFileName[MAX_PATH];
PNGINFO *pPng=Png32_Load(szFileName);
Step 2: Display the image in a HDC
Step 3: Free the occupied memory

The code

//display a 32 bit deep png image
void Png32_Show(HDC hdc,int xDest,int yDest,int nWidth,int nHeight,
       PNGINFO *pPngInfo,int xSour,int ySour)
 if(xSour+nWidth>(int)pPngInfo->nWidth) nWidth=pPngInfo->nWidth-xSour;
 if(ySour+nHeight>(int)pPngInfo->nHeight) nHeight=pPngInfo->nHeight-ySour;
 if(nWidth>0 && nHeight>0)
 HDC hmemdc=0;
 HBITMAP hbmpDest=0;
 HGDIOBJ hOldBmp=0;
 //sour memory
 unsigned char ** row_pointers = 
 //Do alpla blend
 int nLineTailDest=WIDTHBYTES(24*nWidth)-3*nWidth;
 // Initialize header to 0s.
 ZeroMemory(&bmi, sizeof(bmi));
 // Fill out the fields you care about.
 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 bmi.bmiHeader.biWidth = nWidth;
 bmi.bmiHeader.biHeight = nHeight;
 bmi.bmiHeader.biPlanes = 1;
 bmi.bmiHeader.biBitCount = 24;
 bmi.bmiHeader.biCompression = BI_RGB;
 // Create the surface.
 // Get Dest Rectangle memory
 hbmpDest = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, 
            (void **)&pBitsDest, NULL, 0);
#ifdef ASM_CORE
  push esi
  push edi
  push eax
  push ebx
  push ecx
  push edx
  push es
  push ds
  mov ax,ds
  mov es,ax
  mov edi,pBitsDest
  mov ecx,nHeight
  mov ebx, row_pointers
  mov esi,[ebx]
  ;//offset picture's left margin
  mov eax,xSour
  imul eax,4  ;//4 bytes make up one pixel
  add esi,eax 
  mov eax,nWidth  
   ;//get alpla value
   xor ebx,ebx
   mov bl,[esi+3] 
   xor dx,dx
   mov dl,[edi];
   imul dx,bx
   add dh,[esi+2]
   mov [edi],dh ;//save result to *pBitsDest
   inc edi
   xor dx,dx
   mov dl,[edi];
   imul dx,bx
   add dh,[esi+1]
   mov [edi],dh ;//save result to *pBitsDest
   inc edi
   xor dx,dx
   mov dl,[edi];
   imul dx,bx
   add dh,[esi]
   mov [edi],dh ;//save result to *pBitsDest
   inc edi
   add esi,4
   dec eax
   cmp eax,0
   jne beginx
  add edi,nLineTailDest
  sub row_pointers,4 ;//next line
  loop beginy
  pop ds
  pop es
  pop edx
  pop ecx
  pop ebx
  pop eax
  pop edi
  pop esi
  int i,j;
  BYTE *p1=pBitsDest;
   BYTE *p2=*(row_pointers--);
 //Free memory


The new version has a new interface, Png32_LoadFromMem, with which you can easily load a 32 bit PNG image form memory. See CMfc_demoDlg::OnInitDialog for an example.

To test this domo, you should compile the png32_helper project first, and then compile mfc_demo.


  • 2008-04-17

    Add the Png32_LoadFromMem interface.

  • 2007-09-07

    Did pixel pre-multiplication.

  • 2007-07-16

    First submission.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior)
China China
No Biography provided

Comments and Discussions

Questionbuild with mfc static lib Pin
imlamlam3-Oct-13 23:15
Memberimlamlam3-Oct-13 23:15 
GeneralBig problem [modified] Pin
lrtsenar20-Apr-09 4:15
Memberlrtsenar20-Apr-09 4:15 
GeneralRe: Big problem Pin
flyhigh20-Apr-09 15:46
professionalflyhigh20-Apr-09 15:46 
GeneralCompile problem Pin
LuiCifer11-Nov-08 0:00
MemberLuiCifer11-Nov-08 0:00 
GeneralRe: Compile problem Pin
flyhigh11-Nov-08 15:11
professionalflyhigh11-Nov-08 15:11 
GeneralRe: Compile problem Pin
LuiCifer22-Nov-08 3:51
MemberLuiCifer22-Nov-08 3:51 
GeneralRe: Compile problem Pin
flyhigh22-Nov-08 19:18
professionalflyhigh22-Nov-08 19:18 
GeneralProblem in Windows 98 Pin
harishdobhal9-Oct-08 20:50
Memberharishdobhal9-Oct-08 20:50 
GeneralRe: Problem in Windows 98 Pin
flyhigh11-Oct-08 2:29
professionalflyhigh11-Oct-08 2:29 
GeneralRe: Problem in Windows 98 Pin
harishdobhal11-Oct-08 4:30
Memberharishdobhal11-Oct-08 4:30 
GeneralRe: Problem in Windows 98 Pin
flyhigh11-Oct-08 17:03
professionalflyhigh11-Oct-08 17:03 
GeneralRe: Problem in Windows 98 Pin
harishdobhal11-Oct-08 20:49
Memberharishdobhal11-Oct-08 20:49 
GeneralRe: Problem in Windows 98 Pin
flyhigh12-Oct-08 0:01
professionalflyhigh12-Oct-08 0:01 
GeneralRe: Problem in Windows 98 Pin
harishdobhal12-Oct-08 21:14
Memberharishdobhal12-Oct-08 21:14 
GeneralAgain the problem Pin
harishdobhal7-Oct-08 0:46
Memberharishdobhal7-Oct-08 0:46 
GeneralRe: Again the problem Pin
flyhigh7-Oct-08 16:02
professionalflyhigh7-Oct-08 16:02 
GeneralRe: Again the problem Pin
harishdobhal13-Oct-08 1:44
Memberharishdobhal13-Oct-08 1:44 
GeneralRe: Again the problem Pin
flyhigh13-Oct-08 15:58
professionalflyhigh13-Oct-08 15:58 
GeneralRe: Again the problem Pin
harishdobhal22-Oct-08 1:33
Memberharishdobhal22-Oct-08 1:33 
GeneralRe: Again the problem Pin
flyhigh25-Oct-08 3:20
professionalflyhigh25-Oct-08 3:20 
GeneralRe: Again the problem Pin
harishdobhal3-Nov-08 20:06
Memberharishdobhal3-Nov-08 20:06 
QuestionAdvantage/benchmarks of ASM vs AlphaBlend? 64-bit? Pin
Leo Davidson9-Sep-08 21:59
MemberLeo Davidson9-Sep-08 21:59 
What's the advantage of using your ASM vs using AlphaBlend itself? Have you done benchmarks to show that it's worth doing? I'd expect AlphaBlend to be very fast if it's hardware accelerated and it may still be fast assembler even when not.

Is using your own assembler a barrier to compiling the code as 64-bit?
AnswerRe: Advantage/benchmarks of ASM vs AlphaBlend? 64-bit? Pin
flyhigh9-Sep-08 23:05
professionalflyhigh9-Sep-08 23:05 
GeneralPng32_MakeRgn Problem Pin
harishdobhal30-Apr-08 7:45
Memberharishdobhal30-Apr-08 7:45 
GeneralRe: Png32_MakeRgn Problem Pin
flyhigh30-Apr-08 21:33
professionalflyhigh30-Apr-08 21:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.