Click here to Skip to main content
11,481,404 members (63,047 online)
Click here to Skip to main content

Tagged as

Fishy Fishy Fish

, 2 Apr 2010 CPOL 43.6K 4.7K 83
Rate this:
Please Sign up or sign in to vote.
Just for fun; a bunch of fish swimming around the screen

Introduction

This article is a bit of a homage to Davidwu's excellent article, A lovely goldfish desktop pet. In that article, David demonstrates using alpha-blending and GDI+ to make a little fish swim across the screen.

I've taken that code and extended it to support multiple fish along with a SysTray icon to control the number of fish in your tank.

screenshot2.png

I also added some different colored fish images for a little variety.

screenshot.jpg

Using the Code

The code is largely the same as the original, though I've moved things around a bit in order to support multiple fish. Unlike the original code, frames are extracted from the source PNG at startup rather than at each timer tick.

class Frameset : List<Bitmap>, IDisposable
{
    public Frameset(Bitmap b, int framecount)
    {
        if (!Bitmap.IsCanonicalPixelFormat(b.PixelFormat) || 
              !Bitmap.IsAlphaPixelFormat(b.PixelFormat))
            throw new ApplicationException("The picture must be 32bit
                  picture with alpha channel.");

        FrameWidth = b.Width / framecount;
        FrameHeight = b.Height;

        for (int i = 0; i < framecount; i++)
        {
            Bitmap bitmap = new Bitmap(FrameWidth, FrameHeight);
            using (Graphics g = Graphics.FromImage(bitmap))
                g.DrawImage(b, new Rectangle(0, 0, FrameWidth, FrameHeight),
                   new Rectangle(FrameWidth * i, 0, FrameWidth, FrameHeight), 
                   GraphicsUnit.Pixel);

            Add(bitmap);
        }
    }

    public int FrameWidth { get; private set; }

    public int FrameHeight { get; private set; }

    public void Dispose()
    {
        foreach (Bitmap f in this)
            f.Dispose();

        Clear();
    }
}

This greatly reduces the CPU usage for the animation which is important with a whole tankful swimming around. There is also a Form derived from the main FishForm to host the NotifyIcon in the system tray. An instance of this form will always be the first one created. The NotifyIcon context menu allows the user to add and remove fish, show and hide all the fish and of course exit the application.

.NET 4

The project files are all in VS.NET 2010 format, but the only .NET 4 type used is the Tuple. Sacha posted a version of a Tuple in the comments below. So if you want to play with this in 2008 and .NET 3.5, you'll need to incorporate that snippet or otherwise replace the Tuple usage, which shouldn't be too hard.

Conclusion

Oh, and I kind of cheated on the fish colorizing. I color shifted the source PNG in Paint.Net and saved each one as a new set of images. That's why the project size is large. Perhaps someday I'll correct that short cut but for now my apologies for the extra large download. Smile | :)

That's about all there is to it. It's been a fun little project to play around with (and the family and friends I've given it to have enjoyed it as well). This will probably be my last WinForms article. I've caught the WPF bug and am finally cresting the learning curve.

History

  • 3/31/2010 - Initial post

License

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

Share

About the Author

Don Kackman
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10
It only went downhill from there.

Hey look, I've got a blog
Follow on   Twitter

Comments and Discussions

 
QuestionFantastic !! 5 of 2 ;) !! Pin
tvks16-Sep-14 11:26
membertvks16-Sep-14 11:26 
AnswerRe: Fantastic !! 5 of 2 ;) !! Pin
Don Kackman16-Sep-14 15:55
memberDon Kackman16-Sep-14 15:55 
QuestionItem1 does not exist Pin
Sivaji156515-Feb-14 0:17
memberSivaji156515-Feb-14 0:17 
AnswerRe: Item1 does not exist Pin
Don Kackman16-Feb-14 3:35
memberDon Kackman16-Feb-14 3:35 
GeneralVery nice article Pin
Sivaji156515-Feb-14 0:07
memberSivaji156515-Feb-14 0:07 
QuestionThis smells... Pin
Zac Greve5-Jan-13 4:25
memberZac Greve5-Jan-13 4:25 
AnswerRe: This smells... Pin
Don Kackman16-Jan-13 13:47
memberDon Kackman16-Jan-13 13:47 
GeneralMy vote of 5 Pin
Peter Hawke20-Dec-11 15:52
memberPeter Hawke20-Dec-11 15:52 
GeneralMy vote of 5 Pin
Pravin Patil, Mumbai11-Oct-11 23:32
memberPravin Patil, Mumbai11-Oct-11 23:32 
GeneralMy vote of 5 Pin
rahulrcn9-Jun-11 0:29
memberrahulrcn9-Jun-11 0:29 
GeneralMy vote of 5 Pin
ARon_23-Mar-11 5:44
memberARon_23-Mar-11 5:44 
Generalgood one Pin
Pranay Rana20-Dec-10 2:47
memberPranay Rana20-Dec-10 2:47 
GeneralMy vote of 5 Pin
skumar0194-Dec-10 2:49
memberskumar0194-Dec-10 2:49 
GeneralMy vote of 5 Pin
OrganicHuman17-Jul-10 1:26
memberOrganicHuman17-Jul-10 1:26 
GeneralRe: My vote of 5 Pin
Don Kackman23-Jul-10 5:06
memberDon Kackman23-Jul-10 5:06 
Questiontuple? Pin
Christ Kennedy1-Apr-10 2:43
memberChrist Kennedy1-Apr-10 2:43 
AnswerRe: tuple? Pin
Sacha Barber1-Apr-10 3:21
mvpSacha Barber1-Apr-10 3:21 
GeneralRe: tuple? Pin
Don Kackman1-Apr-10 3:34
memberDon Kackman1-Apr-10 3:34 
AnswerRe: tuple? Pin
Sacha Barber1-Apr-10 3:53
mvpSacha Barber1-Apr-10 3:53 
GeneralRe: tuple? Pin
Christ Kennedy1-Apr-10 5:26
memberChrist Kennedy1-Apr-10 5:26 
GeneralRe: tuple? Pin
Sacha Barber1-Apr-10 5:31
mvpSacha Barber1-Apr-10 5:31 
GeneralThats.. Pin
Gary Noble31-Mar-10 23:38
memberGary Noble31-Mar-10 23:38 
GeneralRe: Thats.. Pin
Don Kackman1-Apr-10 7:22
memberDon Kackman1-Apr-10 7:22 
GeneralLike it, and maybe you will like this one too Pin
Sacha Barber31-Mar-10 22:41
mvpSacha Barber31-Mar-10 22:41 
GeneralRe: Like it, and maybe you will like this one too Pin
Don Kackman1-Apr-10 4:04
memberDon Kackman1-Apr-10 4:04 
GeneralRe: Like it, and maybe you will like this one too Pin
Sacha Barber1-Apr-10 4:12
mvpSacha Barber1-Apr-10 4:12 
GeneralRe: Like it, and maybe you will like this one too Pin
Don Kackman25-Apr-10 14:52
memberDon Kackman25-Apr-10 14:52 
GeneralRe: Like it, and maybe you will like this one too Pin
Sacha Barber25-Apr-10 21:09
mvpSacha Barber25-Apr-10 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web02 | 2.8.150520.1 | Last Updated 2 Apr 2010
Article Copyright 2010 by Don Kackman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid