如何在运行时绘制图片框?

时间:2010-12-30 13:38:46

标签: c# picturebox redraw

 public void read_file(string fname)
    {
        filepath = fname;

        TextReader input = File.OpenText(filepath);
        line = input.ReadLine();
        int[] ID = new int[6];
        int[] x = new int[6];
        int[] y = new int[6];
        int xx, yy;
        int i = 0;
        image = AForge.Imaging.Image.FromFile(filename);
        smal1 = AForge.Imaging.Image.FromFile(smallimg1);
        smal2 = AForge.Imaging.Image.FromFile(smallimg2);
        smal3 = AForge.Imaging.Image.FromFile(smallimg3);
        smal4 = AForge.Imaging.Image.FromFile(smallimg4);
        smal5 = AForge.Imaging.Image.FromFile(smallimg5);
        smal6 = AForge.Imaging.Image.FromFile(smallimg6);


        //  int index = 0;
        while (line != null && line != " ")
        {


            if (line == "change")
            {
                while (line != "end")
                {

                    line = input.ReadLine();
                    line = line.Trim();
                    if (line != "end")
                    {
                        string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        xx = int.Parse(parts[0]);
                        yy = int.Parse(parts[1]);
                        image.SetPixel(xx, yy, Color.Blue);
                    }

                }
                line = input.ReadLine();
            }
            else
            {
                string[] parts2 = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                ID[i] = int.Parse(parts2[0]);
                x[i] = int.Parse(parts2[1]);
                y[i] = int.Parse(parts2[2]);
                line = input.ReadLine();
                if(ID[i]==1)
                {
                    pictureBox2.Size = smal1.Size;
                    pictureBox2.Image = smal1;
                }
                else if (ID[i] == 2)
                {
                    pictureBox3.Size = smal2.Size;
                    pictureBox3.Image = smal2;
                }
                else if (ID[i] == 3)
                {
                    pictureBox4.Size = smal3.Size;
                    pictureBox4.Image = smal3;
                }
                else if (ID[i] == 4)
                {
                    pictureBox5.Size = smal4.Size;
                    pictureBox5.Image = smal4;
                }
                else if (ID[i] == 5)
                {
                    pictureBox6.Size = smal5.Size;
                    pictureBox6.Image = smal5;
                }
                else if (ID[i] == 6)
                {
                    pictureBox7.Size = smal6.Size;
                    pictureBox7.Image = smal6;
                }
                    i++;
            }
        }
       // pictureBox2.BackColor = Color.SteelBlue;
       // pictureBox2.Image = smal;
       // pictureBox2.Size = smal.Size;
        pictureBox1.Image = image;
        pictureBox1.Size = image.Size;
        //pictureBox2.Hide();
    }

    public Form1()
    { 
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Start_Click(object sender, EventArgs e)
    {

        capture_flag = true;
        start_reading();

    }

    private void Stop_Click(object sender, EventArgs e)
    {
        capture_flag = false;
    }

    private void start_reading()
    {

        string filen = "F:/4th year/1st Term/sensor network/proj/reconstructscene/stream.txt";
        read_file(filen);
        filen = "F:/4th year/1st Term/sensor network/proj/reconstructscene/stream1.txt";
        read_file(filen);
        filen = "F:/4th year/1st Term/sensor network/proj/reconstructscene/stream2.txt";
        read_file(filen);

    }

    private void close_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

在上面的代码中,我试图多次调用函数read_file但是它不起作用 表格只是等到结束然后画一次。

注意在原始项目中我将从项目的另一部分获取文件名,我可以调用函数

while (capture_flag)
        {

        filen = file;
        read_file(filen);
        }

由于同样的原因不起作用

1 个答案:

答案 0 :(得分:1)

方法运行时没有显示更改的原因是主线程忙于运行方法,因此它不会监听消息。更新以消息队列中的消息的形式发生,因此当主线程无法处理消息时,没有更新。

您可以使用Refresh方法在方法中间强制重绘控件:

pictureBox2.Size = smal1.Size;
pictureBox2.Image = smal1;
pictureBox2.Refresh();