C# Tutorial – Make a Colour Rain Particles Effect in .Net Windows Form and Visual Studio
- Subject: C# Tutorials
- Learning Time: 1 hour
Hi In this tutorial we will be making a cool colourful rain particle effect using Windows Form in .Net and C#. Particle effects are really cool and its even better when we are able to create the effects ourselves. Now doing this windows form was a different kind of fun where you can make something with a smooth animation and have hundreds of different particles flying across the screen its an amazing result. Hope you like this tutorial we worked really hard to make. This was is special because we will finally look into how to load separate images from a folder, save it to a list and then use that list to animate each of the images on the screen dynamically. All of those images will be drawn independently on the screen using the paint event. This kind of effects can be used for lots of games levels and even have the characters interact with the images being spawned on the screen. Its very useful to understand how these particle systems can be created using only Native .net and c# programming in visual studio.
Lesson objectives –
- Create a colourful rain particle effect in windows form and visual studio
- Use OOP classes to create the rain effects and spawn each object to the screen dynamically
- Keep track of the rain effects on the form
- Animate the objects using paint and timer events in windows form
- Dynamically load and assign different coloured rain images to the particles created on the screen
- Assign different speed and location to each of the rain images
- Compile the application and see the final result
Video Tutorial
Download the rain particle effects and background here
Source Code Form 1
namespace Colour_Rain_Effect_Windows_Form_MOO_ICT { public partial class Form1 : Form { // created by mooict.com 2022 // educational purpose only List files = new List(); List particles = new List(); Random random = new Random(); public Form1() { InitializeComponent(); LoadImages(); } private void LoadImages() { files = Directory.GetFiles("images", "*.png").ToList(); for (int i = 0; i < 400; i++) { Particles new_particle = new Particles(files[random.Next(0, files.Count)]); particles.Add(new_particle); } } private void ParticleTimerEvent(object sender, EventArgs e) { foreach (Particles tempImage in particles.ToList()) { tempImage.posY += tempImage.speed; tempImage.posX += 8; if (tempImage.posY > this.ClientSize.Height + 100) { tempImage.speed = random.Next(5, 15); tempImage.posY = -100; tempImage.posX = random.Next(-200, 600); } } this.Invalidate(); } private void FormPaintEvent(object sender, PaintEventArgs e) { Graphics Canvas = e.Graphics; foreach (Particles tempImage in particles.ToList()) { Canvas.DrawImage(tempImage.particle, tempImage.posX, tempImage.posY, tempImage.width, tempImage.height); } } } }
Source Code Rain Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace Colour_Rain_Effect_Windows_Form_MOO_ICT { internal class Particles { // created by mooict.com 2022 // for educational purpose only public int height; public int width; public int posX; public int posY; public int[] sizes = { 30, 40, 50 }; Random random = new Random(); public int speed = 0; int i = 0; public Image particle; public Particles(string fileLocation) { particle = Image.FromFile(fileLocation); i = random.Next(0, sizes.Length); height = sizes[i]; width = sizes[i]; posX = random.Next(-200, 800); posY = random.Next(-700, -200); speed = random.Next(1, 10); } } }