简体   繁体   English

C#winforms使用集合绘制到图片框的问题?

[英]C# winforms problems drawing to picturebox using collection?

The form contains a picturebox (picturebox1) and a timer control (timer1)... 该表单包含一个图片框(picturebox1)和一个计时器控件(timer1)...

Basically at start up I create a List collection containing 5 instances of my pixel class. 基本上,在启动时,我创建一个包含5个像素类实例的List集合。 When the timer triggers I call a refresh of the picturebox which calls the paint event. 当计时器触发时,我调用PictureBox的刷新,该刷新称为Paint事件。 In the paint event I iterate through the list collection and call each pixel's draw method. 在绘画事件中,我遍历列表集合并调用每个像素的draw方法。

The problem I am having is that only one pixel shows up...that is.. unless I set a break at the point where I am adding the pixel, continue, and break again and repeat until all pthe pixels are created. 我遇到的问题是只有一个像素出现......除非我在添加像素的位置设置一个中断,然后继续,然后再次中断并重复直到创建所有像素。 Then for some reason all of the pixels show up... 然后由于某种原因所有像素都出现了...

Can someone tell me why I can only see a single pixel? 有人可以告诉我为什么我只能看到一个像素吗?

public partial class Form1 : Form
{
    List<Pixel> pixels = new List<Pixel>();

    public Form1()
    {            
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int ii = 0; ii < 5; ii++)
            pixels.Add(new Pixel(pictureBox1));  // <- breakpoint here...?
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        foreach (Pixel p in pixels)
            p.Draw(e, pictureBox1);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox1.Refresh();
    }
}

public class Pixel
{
    Random r = new Random(DateTime.Now.Millisecond);

    public Point Position { get; set; }

    public Pixel(PictureBox src) 
    {
        Position = new Point(r.Next(0, src.Width), r.Next(0, src.Height));
    }

    public void Draw(PaintEventArgs e, PictureBox src)
    {
        e.Graphics.DrawRectangle(new Pen(Color.Black), Position.X, Position.Y, 1, 1);
    }
}

My original code does a lot more.. but I stripped it all out and got the same results. 我的原始代码做得更多。.但是我将其全部剥离并获得了相同的结果。

Since Random r = new Random(DateTime.Now.Millisecond) was called each time with the same seed value the pixels showed up in exactly the same spot. 由于每次以相同的种子值调用Random r = new Random(DateTime.Now.Millisecond)时,像素出现在完全相同的位置。 I moved the Random declaration to the main class and passed it into the pixel class. 我将Random声明移至主类,并将其传递给pixel类。 Now it works as I expect. 现在,它按我的预期工作。

public partial class Form1 : Form
{
    Random r = new Random(DateTime.Now.Millisecond);
    List<Pixel> pixels = new List<Pixel>();

    public Form1()
    {            
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int ii = 0; ii < 5; ii++)
        {
            pixels.Add(new Pixel(pictureBox1, r));
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        foreach (Pixel p in pixels)
            p.Draw(e, pictureBox1);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox1.Refresh();
    }
}

public class Pixel
{
    public Point Position { get; set; }

    public Pixel(PictureBox src, Random r) 
    {
        Position = new Point(r.Next(0, src.Width), r.Next(0, src.Height));
    }

    public void Draw(PaintEventArgs e, PictureBox src)
    {
        e.Graphics.DrawRectangle(new Pen(Color.Black), Position.X, Position.Y, 1, 1);
    }
}

This is because your randomizer on all 5 instances of your pixel class is using the exact same seed (it is all happening on the same millisecond value). 这是因为您的像素类的所有5个实例上的随机化器都使用完全相同的种子(都发生在相同的毫秒值上)。 Not to condone the use of Thread.Sleep, the quickest "fix" to at least expose the problem would be to change your form_load event to look like the following (you could also refactor how you're randomizing): 不要纵容Thread.Sleep的使用,至少可以暴露问题的最快“修复”方法是将form_load事件更改为如下所示(您也可以重构随机方式):

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int ii = 0; ii < 5; ii++)
        {
            Thread.Sleep(1);
            pixels.Add(new Pixel(pictureBox1));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM