Click here to Skip to main content
13,349,539 members (53,544 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked
Posted 18 Oct 2005

Create animation in dotNet with alpha-blending PNG images by calling API

, 6 Jul 2007
Rate this:
Please Sign up or sign in to vote.
This article shows you the advantage of PNG images with alpha-blending feature in creating animation and demonstates how to use PNG images in Framework.NET application by calling API

Sample Image - revolution.jpg


A lot of coders (including myself before) have given up working with PNG because it's not so easy to make PNG background image to work smoothly. However, PNG is a very good image format which allows you to use transparency with alpha-blending feature. This article will show you how to make PNG work and the advantage of using it in creating animation.

Secret inside

clsTungPngAnimation inherits the abstract class clsTungAnimation since we intend to have many of the same functions in clsTungPngAnimation and clsTungBmpAnimation. All these classes can be found in clsTungAnimation.cs

frmAnimation.cs (for PNG) and frmBmpAnimation.cs (for BMP) are where picture frames will appear in. If we use PNG, this function must be used to make the form transparent but still can keep alpha-blending of PNG pictures:

protected override CreateParams CreateParams
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x00080000; //WS_EX_LAYERED    
        return cp;

(This function is inside frmAnimation.cs)

clsBitmapTransparent.cs is used to create PNG transparent background for the form.

clsBitmapRegion.cs is used to create a region of a form from loading BMP images so that images will appear transparent.

Trung tam di dong qui tu cac cua hang lon nhat Vietnam


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Team Leader
Vietnam Vietnam


Full Name : Nguyen Thanh Tung
Sex : Male
Date of Birth : October 06, 1983
Nationality : Vietnamese


Trustworthy, efficient, capable work under pressure, outgoing person and capable work in a team.


    • Proficient in Programming: .Net 1.0/1.1/2.0/3.0/3.5 (C#, VB.Net, ASP.Net,
      AJAX, WCF, WWF), VB4/5/6, ASP, PHP, HTML, CSS, Script, XML.
      Experience with Vs Source Safe, Team System, MsBuild, SVN/CVS. Knowledge about
      UnitTest, Test-Driven development.
    • Experience with Databases: SQL Server 2000/2005, MySQL
      4.x/5.x/6.x, Oracle, PostgreSQL, Ms. Access.
    • Experience in EIS: ERP, CRM, SCM ...
    • Experience with Microsoft Products: CRM3/4, Sharepoint
      2003/2007 ...
    • Proficient in OS: Linux (Redhat, Fedora, Mandrake, Suse), Windows (2000/2003
      Server, XP, Vista).
    • Proficient in Graphics (Flash, Photoshop, 3DMax …); Microsoft Office, Visio,
      Microsoft Project.
    • Proficient in Network Devices: Router, Switch, Firewall Server, Mail Server,
      FTP Server, DNS, VPN.
      Experience in Servers: IBM, HP, Sun, Dell ...
    • Familiar with OO and SOA concepts.
    • Knowledge about project management: CMMI, Agile and Scrum, Software
      Engineering, System Analysis and Design, IT Project Management. 
    • Knowledge in Accounting and Finance: Financial Statement, Income
      Statement ...


    • SMS Gateway.
    • POP3-IMAP-SMTP App.
    • Products Management Sys.
    • Hedge Fund Management Sys.
    • Motion detection.
    • 3D games engine (C# using OpenGL Lib).
    • Video Conference.
    • Information Retrieval.
    • Microsoft CRM3/4
    • Microsoft Sharepoint Portal 2003/2007
    • CMS
    • E-Commerce

You may also be interested in...


Comments and Discussions

GeneralAnother solution, but controls not seen Pin
eusta26-Nov-07 11:02
membereusta26-Nov-07 11:02 
Hello World

The main form:

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms
Imports WindowsApplication1.Win32

Public Class Form1

Protected Overrides ReadOnly Property CreateParams() As CreateParams
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or 524288 'WS_EX_LAYERED
Return cp
End Get
End Property

Protected Overrides Sub WndProc(ByRef m As Message)
If (m.Msg = 132) Then 'WM_NCHITTEST
m.Result = IntPtr.op_Explicit(2) 'HTCLIENT
End If
End Sub

Private Sub SetBackground(ByVal fileName As String)

Dim bitmap As Bitmap = Image.FromFile(fileName)
If bitmap.PixelFormat <> PixelFormat.Format32bppArgb Then
Throw New ApplicationException("must be 32 bits per pixel")
End If

Dim hBitmap As IntPtr = IntPtr.Zero
Dim oldBitmap As IntPtr = IntPtr.Zero
Dim screenDc As IntPtr = Win32.GetDC(IntPtr.Zero)
Dim memDc As IntPtr = Win32.CreateCompatibleDC(screenDc)

hBitmap = bitmap.GetHbitmap(Color.FromArgb(0))
oldBitmap = Win32.SelectObject(memDc, hBitmap)
Dim size As Win32.Size = New Win32.Size(bitmap.Width, bitmap.Height)
Dim pointSource As Win32.Point = New Win32.Point(0, 0)
Dim topPos As Win32.Point = New Win32.Point(Left, Top)
blend.BlendOp = 0
blend.BlendFlags = 0
blend.SourceConstantAlpha = Byte.MaxValue
blend.AlphaFormat = 1
Win32.UpdateLayeredWindow(Handle, screenDc, topPos, size, memDc, pointSource, 0, blend, 2)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
In Class Win32

Namespace movepng

End Namespace

To get the result on the main form.
Unfortunately, the problem remains of controls ... Not seen checks
But if I change the opacity to the main form, the effect disappears and controls can be seen.

I use 2005

How can I do to see the controls?
Thanks Smile | :)
GeneralRe: Another solution, but controls not seen Pin
eusta27-Nov-07 5:57
membereusta27-Nov-07 5:57 
GeneralUmm... Pin
BoneSoft10-Jul-07 6:20
memberBoneSoft10-Jul-07 6:20 
QuestionCan we save these multiple images as a multiframe GIF Image Pin
sachit121-Jul-06 0:51
membersachit121-Jul-06 0:51 
AnswerRe: Can we save these multiple images as a multiframe GIF Image Pin
Nguyê~n Thanh Tùng21-Jul-06 1:48
memberNguyê~n Thanh Tùng21-Jul-06 1:48 
AnswerRe: Can we save these multiple images as a multiframe GIF Image Pin
Nguyê~n Thanh Tùng21-Jul-06 2:39
memberNguyê~n Thanh Tùng21-Jul-06 2:39 
QuestionRe: Can we save these multiple images as a multiframe GIF Image Pin
TejasBhai15-Aug-06 21:17
memberTejasBhai15-Aug-06 21:17 
QuestionControls on the Alpha-Blended Form Pin
Allen G12-Apr-06 3:16
memberAllen G12-Apr-06 3:16 
AnswerRe: Controls on the Alpha-Blended Form Pin
Nguyê~n Thanh Tùng12-Apr-06 5:13
memberNguyê~n Thanh Tùng12-Apr-06 5:13 
QuestionRe: Controls on the Alpha-Blended Form Pin
Assaf Koren21-Apr-06 1:11
memberAssaf Koren21-Apr-06 1:11 
QuestionGIF Version Pin
jumacabo18-Oct-05 22:11
memberjumacabo18-Oct-05 22:11 
AnswerRe: GIF Version Pin
Nguyê~n Thanh Tùng18-Oct-05 22:25
memberNguyê~n Thanh Tùng18-Oct-05 22:25 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 6 Jul 2007
Article Copyright 2005 by nguyenthanhtungtinbk
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid