Click here to Skip to main content
11,935,066 members (58,431 online)
Click here to Skip to main content
Add your own
alternative version


319 bookmarked

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

, 9 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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! Smile | :)

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

You may also be interested in...

Comments and Discussions

PraiseMy vote of 5 Pin
Humayun Shabbir7-Nov-15 21:09
memberHumayun Shabbir7-Nov-15 21:09 
QuestionGreat fish Pin
N. Henrik Lauridsen1-Oct-15 5:45
memberN. Henrik Lauridsen1-Oct-15 5:45 
GeneralMy vote of 5 Pin
JJMatthews24-Oct-12 0:40
memberJJMatthews24-Oct-12 0:40 
Generalnice one Pin
george pradeep12-Mar-12 0:17
membergeorge pradeep12-Mar-12 0:17 
QuestionC++ version Pin
mrx_jims6-Mar-12 1:40
membermrx_jims6-Mar-12 1:40 
GeneralMy vote of 5 Pin
manoj kumar choubey17-Feb-12 0:58
membermanoj kumar choubey17-Feb-12 0:58 
GeneralMy vote of 5 Pin
Harith Randika2-Feb-12 18:14
memberHarith Randika2-Feb-12 18:14 
GeneralMy vote of 5 Pin
Tushar_Patil28-Dec-11 1:09
memberTushar_Patil28-Dec-11 1:09 
QuestionMy Vote of 4 Pin
Clark Kent12330-Nov-11 8:54
memberClark Kent12330-Nov-11 8:54 
GeneralMy vote of 5 Pin
MMChandrasekar12-Nov-11 16:56
memberMMChandrasekar12-Nov-11 16:56 
GeneralMy vote of 5 Pin
red_code20085-Sep-11 20:59
groupred_code20085-Sep-11 20:59 
GeneralMy vote of 5 Pin
jerviz_761-Feb-11 18:05
memberjerviz_761-Feb-11 18:05 
Questionwitch part makes the fish stay on desktop? please tell me.thank you very much. [modified] Pin
luangeng21-Sep-10 18:10
memberluangeng21-Sep-10 18:10 
GeneralC++ version to David Wu contribution. [modified] Pin
hirohata13-Jul-10 4:22
memberhirohata13-Jul-10 4:22 
GeneralReally cute Pin
Bigdeak10-Jun-10 5:55
memberBigdeak10-Jun-10 5:55 
GeneralVery beautiful Pin
kuyexing3-Jun-10 23:12
memberkuyexing3-Jun-10 23:12 
GeneralGood job!,but it is CPU-bounded application. Pin
nblijun3-Apr-10 17:22
membernblijun3-Apr-10 17:22 
GeneralMy vote of 5 Pin
Smithers-Jones8-Feb-10 1:10
memberSmithers-Jones8-Feb-10 1:10 
JokeRe: My vote of 5 Pin
Smithers-Jones10-Feb-10 3:07
memberSmithers-Jones10-Feb-10 3:07 
GeneralTV Logo Pin
divyesh14324-Feb-10 20:45
memberdivyesh14324-Feb-10 20:45 
Generalc++ code for the same Pin
Member 35644839-Jan-10 21:14
memberMember 35644839-Jan-10 21:14 
Generalgood job Pin
fei_cong9-Dec-09 19:43
memberfei_cong9-Dec-09 19:43 
GeneralWOW. Pin
francis321-Sep-09 21:40
memberfrancis321-Sep-09 21:40 
GeneralLovely Pin
equipmentloannswguru5-Sep-09 1:08
memberequipmentloannswguru5-Sep-09 1:08 
GeneralAmazing!!!! Pin
oe30e30e303e12-Aug-09 4:51
memberoe30e30e303e12-Aug-09 4:51 
QuestionWhere does the goldfish image come from? Pin
gapman22-Jul-09 20:26
membergapman22-Jul-09 20:26 
Generalnice job Pin
pkfoo11-May-09 3:29
memberpkfoo11-May-09 3:29 
General[Message Removed] Pin
Katekortez25-Oct-08 10:08
memberKatekortez25-Oct-08 10:08 
GeneralCool! Made some additions... [modified] Pin
Don Kackman21-Sep-08 11:26
memberDon Kackman21-Sep-08 11:26 
Generalhi Pin
srk-61921-Sep-08 6:58
membersrk-61921-Sep-08 6:58 
GeneralNice Article Pin
Reese17-Sep-08 7:45
memberReese17-Sep-08 7:45 
GeneralNice Work! Pin
Shakeel Iqbal15-Sep-08 19:29
memberShakeel Iqbal15-Sep-08 19:29 
Generalthanks ! Pin
BillWoodruff15-Sep-08 18:05
memberBillWoodruff15-Sep-08 18:05 
GeneralWonderful Pin
wangwenyu15-Sep-08 15:52
memberwangwenyu15-Sep-08 15:52 
QuestionExcellent but...! Pin
VainsofJenna14-Sep-08 1:17
memberVainsofJenna14-Sep-08 1:17 
AnswerRe: Excellent but...! Pin
Davidwu15-Sep-08 4:49
memberDavidwu15-Sep-08 4:49 
GeneralRe: Excellent but...! Pin
luangeng21-Sep-10 18:13
memberluangeng21-Sep-10 18:13 
GeneralSize does mater ! Pin
Nicolas MEURET11-Sep-08 4:19
memberNicolas MEURET11-Sep-08 4:19 
GeneralRe: Size does matter ! Pin
RVW19-Sep-08 5:47
memberRVW19-Sep-08 5:47 
QuestionWhere is the executable or demo? Pin
Ilíon9-Sep-08 6:22
memberIlíon9-Sep-08 6:22 
AnswerRe: Where is the executable or demo? Pin
Davidwu9-Sep-08 6:43
memberDavidwu9-Sep-08 6:43 
GeneralRe: Where is the executable or demo? Pin
Ilíon9-Sep-08 7:54
memberIlíon9-Sep-08 7:54 
Generalnice work! Pin
godviser9-Sep-08 4:58
membergodviser9-Sep-08 4:58 
NewsProblem with website referenced in code Pin
tlhintoq9-Sep-08 4:47
membertlhintoq9-Sep-08 4:47 
GeneralRe: Problem with website referenced in code Pin
Davidwu9-Sep-08 6:09
memberDavidwu9-Sep-08 6:09 
Generalwonderful. 5 star Pin
Tim Aderson9-Sep-08 3:33
memberTim Aderson9-Sep-08 3:33 
GeneralWow Pin
JobsFunny9-Sep-08 2:17
memberJobsFunny9-Sep-08 2:17 
GeneralVery nice job! Pin
domz20089-Sep-08 2:10
memberdomz20089-Sep-08 2:10 
GeneralNice looking fish, but... Pin
Dave Kreskowiak9-Sep-08 1:20
mvpDave Kreskowiak9-Sep-08 1:20 
GeneralRe: Nice looking fish, but... Pin
JobsFunny9-Sep-08 2:31
memberJobsFunny9-Sep-08 2:31 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 9 Sep 2008
Article Copyright 2008 by Davidwu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid