如何在运行时绘制图片框? [英] How do I draw a picturebox at run time?

查看:81
本文介绍了如何在运行时绘制图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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,但是它不起作用 表格只是等待到最后,然后画一次.

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

推荐答案

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

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.

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

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

这篇关于如何在运行时绘制图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆