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

发布于 2024-10-09 17:19:35 字数 4145 浏览 3 评论 0原文

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

因为同样的原因该函数不起作用

 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-10-16 17:19:35

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

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文