在ListBox中看不到结果

时间:2012-08-01 10:29:23

标签: c# .net winforms

我有一段C#代码:

private void btn_getPixels_Click_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    listBox1.Items.Add("Pixel             Color");
    try
    {
        Bitmap img = new Bitmap(pictureBox1.Image);
        Color c;

        for (int i = 0; i < img.Width; i++)
        {
            for (int j = 0; j < img.Height; j++)
            {
                c = img.GetPixel(i, j);
                listBox1.Items.Add(i + "," + j + "   " + c.Name);
            }
        }

        MessageBox.Show("SUCESSFULLY DONE");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

问题是外部循环完成后,我在listBox1中看不到任何结果。

知道如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

您的代码成功运行(我已检查过)。似乎因为img.Widthimg.Height是大值而您的程序仍然有效(因此您无法看到结果)。尝试这个循环,看看结果:

    for (int i = 0; i < 50; i++)
    {
        for (int j = 0; j < 50; j++)
        {
            ...

答案 1 :(得分:3)

我的代码没有任何问题: enter image description here

当然,你必须改变宽度和高度才能进行测试。

相关问题