65.9K
CodeProject is changing. Read more.
Home

Snowfall in WPF Application

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (8 votes)

Dec 16, 2014

CPOL
viewsIcon

29126

downloadIcon

1033

Snowfall animation in WPF application. Bring some winter to your application!

Introduction

Nowadays, it's very hard to find snowfall animation for WPF like in Stone age, but what if you want to give your users some winter or Christmas mood? Here is one of the simplest ways to do it.

and a real example:

Background

The principle is very simple - the code uses CompositionTarget.Rendering event and redraws snowflakes in new position with simple and understandable algorithm.

Using the Code

The usage is very simple too:

        private readonly SnowEngine snow = null;

        public MainWindow()
        {
            InitializeComponent();
            snow = new SnowEngine(canvas, "pack://application:,,,/Graphics/snow1.png",
               "pack://application:,,,/Graphics/snow2.png",
               "pack://application:,,,/Graphics/snow3.png",
               "pack://application:,,,/Graphics/snow4.png",
               "pack://application:,,,/Graphics/snow5.png",
               "pack://application:,,,/Graphics/snow6.png",
               "pack://application:,,,/Graphics/snow7.png",
               "pack://application:,,,/Graphics/snow8.png",
               "pack://application:,,,/Graphics/snow9.png");
            snow.Start();
        }

Window or another control must have Canvas you pass to SnowEngine and images of flakes. It can be resources as shown above.

    <Grid>
        <Canvas Name="canvas" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </Grid>

In addition, you can change some properties in the class:

  • Horizontal and vertical snow speed
  • Minimum and maximum radius of flakes to make them different
  • Percentage of snow coverage

Points of Interest

Pros

  • Easy to use
  • Pretty looking

Cons

  • Not stable animation speed because WPF renders it by his own mind depending on computer load and video card power

History

v1.0.0.1

  • Fixed SnowCoverage property don't set value
  • canvas.IsHitTestVisible = false sets in SnowEngine constructor