How do I draw a picturebox at run time?

放肆的年华 提交于 2019-12-12 01:23:52

问题


 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 .

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


回答1:


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:

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


来源:https://stackoverflow.com/questions/4563014/how-do-i-draw-a-picturebox-at-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!