如何使用 C# 在单击按钮时将同一图片的另一张图片添加到我的表单?

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

我创建了一个 windows 表单,当我点击一个按钮时,它会显示一只熊猫在移动。 当我添加一只熊猫时它有效,但我希望在单击按钮时出现另一只熊猫。当我再次单击按钮以显示另一只熊猫时,我正在尝试!当我点击按钮时,我的熊猫从它的起点消失并再次出现并开始移动!

(例如点击按钮 3 次 = 有 3 pandas 在我的表单中移动)

那是 class 的代码 "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;
        }       
    }
}

这是我用于表单的代码:

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;
        }
    }
}

Form1_Paint只画一只熊猫。您需要以某种方式保存您创建的所有 pandas,并在 Paint 方法中绘制所有 pandas!您还需要更新 Tick.

中所有 pandas 的位置

例如: 在 class List<panda> pandas;

中定义一个成员
    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));
        pandas.Add(p);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (panda p in pandas)
        {
            Rectangle rect = p.rect_panda;
            // Fix the rect like before
        }
    }

与 Draw 类似,遍历 pandas 并绘制每个。这能帮助您摆脱困境吗?

试试这个:

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)));
    }
}