简体   繁体   English

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

[英]How do I draw a picturebox at run time?

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

in the above code i'm trying to call the function read_file several times but it's not working the form just wait until the end then draw once . 在上面的代码中,我试图多次调用函数read_file,但是它不能正常工作,只是等到最后再画一次。

note in the original project i'll take the file name from another part of the poject and i may call the function like 请注意,在原始项目中,我将从文件的另一部分获取文件名,并且我可能会像

while (capture_flag)
        {

        filen = file;
        read_file(filen);
        }

which not working for the same reason 由于相同的原因而无法正常工作

The reason that the changes doesn't show up whlie the method is running, is that the main thread is busy running the method so it's not listening to messages. 在方法运行时未显示更改的原因是主线程正在忙于运行该方法,因此它不监听消息。 The updates happens in the form of messages that are placed on the message queue, so when the main thread can't handle the messages, there are no updates. 更新以放置在消息队列中的消息的形式进行,因此,当主线程无法处理消息时,就不会有更新。

You can use the Refresh method to force a redraw of the control while in the middle of the method: 您可以在方法中间使用Refresh方法强制重绘控件:

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

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

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