Search code examples
c#windowswindows-forms-designersystem.drawing

How to add another picture of the same picture to my form on button click using C#?


I've created a windows form that when I click on a button, it shows a panda moving. It works when I add one Panda, but I want another panda to appear on button click. I'm trying to when I click the button for another time to show another panda! When I click the button my panda disappear and reappear again from it's start point and starts moving!

(for example clicking the button 3 times = having 3 pandas moving in my form)

That's the code for the class called "panda":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace test_moving_pic
{   
    class panda
    {
        public Image img_panda;
        public Rectangle rect_panda;
        
        public panda(Image img, Rectangle rect)
        {
            this.img_panda = img;
            this.rect_panda = rect;
        }       
    }
}

and this is the code that I used for my Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test_moving_pic
{
    public partial class Form1 : Form
    {
        Image image;
        Rectangle rect;

        int direction = 3;
        public Form1()
        {
            InitializeComponent();           
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
           Graphics g = e.Graphics;
            if (image != null && rect != null)
                g.DrawImage(image, rect);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            rect.X += this.direction;
            rect.Y += this.direction;

            if (rect.X <= 100 && rect.Y <= 100)
            {
                rect.X += this.direction;
                rect.Y += this.direction;
            }
            else
            {
                rect.Y += this.direction;
                if (rect.Y >= 100)
                {
                    rect.Y = 100;
                    rect.X += this.direction;
                }
            }
            
            Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panda p = new panda(Image.FromFile("C:\\Users\\hsnha\\OneDrive\\Desktop\\Panda.png"), new Rectangle(20, 20, 70, 70));
            image = p.img_panda;
            rect = p.rect_panda;
        }
    }
}


Solution

  • Try this:

    public partial class Form1 : Form
    {
        List<panda> pandaList = new List<panda>();
        int direction = 3;
    
        class panda
        {
            public Image img_panda;
            public Rectangle rect_panda;
    
            public panda(Image img, Rectangle rect)
            {
                this.img_panda = img;
                this.rect_panda = rect;
            }
        }
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            foreach (panda p in pandaList)
                g.DrawImage(p.img_panda, p.rect_panda);
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (panda p in pandaList)
            {
                p.rect_panda.X += this.direction;
                p.rect_panda.Y += this.direction;
    
                if (p.rect_panda.X <= 100 && p.rect_panda.Y <= 100)
                {
                    p.rect_panda.X += this.direction;
                    p.rect_panda.Y += this.direction;
                }
                else
                {
                    p.rect_panda.Y += this.direction;
                    if (p.rect_panda.Y >= 100)
                    {
                        p.rect_panda.Y = 100;
                        p.rect_panda.X += this.direction;
                    }
                }
    
            }
            Invalidate();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            pandaList.Add(new panda(Image.FromFile(@"C:\Users\hsnha\OneDrive\Desktop\Panda.png"), new Rectangle(20, 20, 70, 70)));
        }
    }