Click here to Skip to main content
15,358,946 members
Articles / Multimedia / GDI+
Posted 8 Sep 2008

Tagged as


325 bookmarked

A lovely goldfish desktop pet (using alpha-PNG and GDI+)

Rate me:
Please Sign up or sign in to vote.
4.91/5 (234 votes)
8 Sep 2008CPOL1 min read
Using alpha-PNG and GDI+ technology to implement a desktop pet fish!



This article demonstrates a very lovely goldfish desktop pet, using alpha-PNG and GD+. This cute goldfish can swim from one side to another of your desktop, and tries to escape when you try to catch it using your mouse.


After I posted these articles: A Cool Vista Sidebar Gadget Style CPUInfo Animate Control! (Fixed)A cool Vista-Sidebar-style clock control (4 style)(fixed)! many people asked me how to make a real alpha-transparent form using these controls. Now, this has become a reality! This is a real alpha-transparent form with animation, even in some none-Vista OSs such as XP!

Have fun! And, do not forget to vote! :)

Using the code

To make a form transparent, use this code to set an alpha-transparent PNG picture as its background:

public void SetBits(Bitmap bitmap)
    if (!haveHandle) return;
    if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || 
        throw new ApplicationException("The picture must be " + 
                  "32bit picture with alpha channel");
    IntPtr oldBits = IntPtr.Zero;
    IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
    IntPtr hBitmap = IntPtr.Zero;
    IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
        Win32.Point topLoc = new Win32.Point(Left, Top);
        Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
        Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
        Win32.Point srcLoc = new Win32.Point(0, 0);
        hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
        oldBits = Win32.SelectObject(memDc, hBitmap);
        blendFunc.BlendOp = Win32.AC_SRC_OVER;
        blendFunc.SourceConstantAlpha = 255;
        blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
        blendFunc.BlendFlags = 0;
        Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, 
                         memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
        if (hBitmap != IntPtr.Zero)
            Win32.SelectObject(memDc, oldBits);
        Win32.ReleaseDC(IntPtr.Zero, screenDC);

OK, so simple! Now, you can use a timer control to implement the animation function.

Sorry for my poor English! Please see the source code for more details.

Points of Interest

  • Developing with GDI+ and C# is a very interesting thing!
  • PNG format is very good for drawing alpha pics!
  • For more code samples, please visit my personal web site.



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


About the Author

China China
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
Member 140620971-Jan-19 21:30
MemberMember 140620971-Jan-19 21:30 
Question.NET 4.5 / .NET 4.6 Pin
Member 1331645530-Jul-17 1:59
MemberMember 1331645530-Jul-17 1:59 
Question你好,请问怎样把窗口放中间 Pin
Member 1326641219-Jun-17 2:52
MemberMember 1326641219-Jun-17 2:52 
PraiseMy vote of 5 Pin
Humayun Shabbir7-Nov-15 20:09
MemberHumayun Shabbir7-Nov-15 20:09 
QuestionGreat fish Pin
N. Henrik Lauridsen1-Oct-15 4:45
MemberN. Henrik Lauridsen1-Oct-15 4:45 
GeneralMy vote of 5 Pin
JJMatthews23-Oct-12 23:40
MemberJJMatthews23-Oct-12 23:40 
Generalnice one Pin
george pradeep11-Mar-12 23:17
Membergeorge pradeep11-Mar-12 23:17 
QuestionC++ version Pin
mrx_jims6-Mar-12 0:40
Membermrx_jims6-Mar-12 0:40 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Feb-12 23:58
professionalManoj Kumar Choubey16-Feb-12 23:58 
GeneralMy vote of 5 Pin
Harith Randika2-Feb-12 17:14
MemberHarith Randika2-Feb-12 17:14 
GeneralMy vote of 5 Pin
_Tushar Patil28-Dec-11 0:09
Member_Tushar Patil28-Dec-11 0:09 
QuestionMy Vote of 4 Pin
Clark Kent12330-Nov-11 7:54
professionalClark Kent12330-Nov-11 7:54 
GeneralMy vote of 5 Pin
MMChandrasekar12-Nov-11 15:56
MemberMMChandrasekar12-Nov-11 15:56 
GeneralMy vote of 5 Pin
red_code20085-Sep-11 19:59
Memberred_code20085-Sep-11 19:59 
GeneralMy vote of 5 Pin
jerviz_761-Feb-11 17:05
Memberjerviz_761-Feb-11 17:05 
Questionwitch part makes the fish stay on desktop? please tell me.thank you very much. [modified] Pin
luangeng21-Sep-10 17:10
Memberluangeng21-Sep-10 17:10 
GeneralC++ version to David Wu contribution. [modified] Pin
hirohata13-Jul-10 3:22
Memberhirohata13-Jul-10 3:22 
GeneralReally cute Pin
Bigdeak10-Jun-10 4:55
MemberBigdeak10-Jun-10 4:55 
GeneralVery beautiful Pin
Hsing_Yeh3-Jun-10 22:12
MemberHsing_Yeh3-Jun-10 22:12 
GeneralGood job!,but it is CPU-bounded application. Pin
nblijun3-Apr-10 16:22
Membernblijun3-Apr-10 16:22 
GeneralMy vote of 5 Pin
Smithers-Jones8-Feb-10 0:10
MemberSmithers-Jones8-Feb-10 0:10 
JokeRe: My vote of 5 Pin
Smithers-Jones10-Feb-10 2:07
MemberSmithers-Jones10-Feb-10 2:07 
GeneralTV Logo Pin
divyesh14324-Feb-10 19:45
Memberdivyesh14324-Feb-10 19:45 
Generalc++ code for the same Pin
ravendra0039-Jan-10 20:14
Memberravendra0039-Jan-10 20:14 
Generalgood job Pin
fei_cong9-Dec-09 18:43
Memberfei_cong9-Dec-09 18:43 

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.