S
S
Shayden2021-10-04 04:02:22
Windows Forms
Shayden, 2021-10-04 04:02:22

How to speed up image rendering on picturebox.image and other elements?

There is a program and its form, to which images are loaded from the network. To change the image, I press button.click. However, there is one point, namely: after pressing the button, the next image should appear instantly (in terms of speed, it is similar to flipping through photos in the "Windows Photo Viewer" :))) only with very extremely rare delays). And it appears, but with a delay of one or about one second, and in theory it should not be like this and there are similar programs of mine, on which this is exactly what happens as I need it, without delay! How and is it possible in this code to speed up the rendering / appearance of images on picturebox.image and other elements that appear along with it? Perhaps even in the code there are elements that should not exist at all and you can throw them out of the code:

private void ricVision()
        {
            try
            {
                if (!this.locker && (this.waitVision.Count > 0))
                {
                    this.textBox2.Focus();                   
                    this.locker = true;
                    string[] strArray = this.waitVision[0];
                    if (strArray[9].ToString() != "-")
                    {
                        this.textBox2.Text = strArray[9].ToString();
                    }
                    int num = Convert.ToInt32(strArray[0].ToString());
                    int num2 = Convert.ToInt32(strArray[1].ToString());
                    int index = Convert.ToInt32(strArray[6].ToString());
                    this.visionType = strArray[2].ToString();
                    this.Registor = strArray[3].ToString();
                    this.WordsTwo = strArray[4].ToString();
                    this.label7.Text = this.Registor;
                    this.label8.Text = strArray[4].ToString();
                    this.label16.Text = strArray[10].ToString();
                    {
                        if (label16.Text.Length > 5)
                            label16.Text = label16.Text.Substring(0, 5);
                    };
                    this.label19.Text = strArray[12].ToString();
                    this.label21.Text = this.BASE64;
                    this.label23.Text = strArray[13].ToString();
                    this.BASE64 = strArray[14].ToString();
                    DoubleBuffered = true;
                    if (label7.Text == "False")
                    {
                        dataGridView1.Rows[index].Cells[1].Style.BackColor = Color.Empty;
                        DoubleBuffered = true;
                    }
                    else if (label7.Text == "True")
                    {
                        dataGridView1.Rows[index].Cells[1].Style.BackColor = Color.Tomato;
                        DoubleBuffered = true;
                    }
                    if (label8.Text == "False")
                    {
                        dataGridView1.Rows[index].Cells[3].Style.BackColor = Color.Empty;
                        DoubleBuffered = true;
                    }
                    else if (label8.Text == "True")
                    {
                        dataGridView1.Rows[index].Cells[3].Style.BackColor = Color.Tomato;
                        DoubleBuffered = true;
                    }
                    this.label10.Text = this.visionType;
                    this.pictureBox1.Width = num;
                    this.pictureBox1.Height = num2;
                    this.panel2.Visible = true;
                    {
                        this.label13.Text = this.dataGridView1.Rows[index].Cells[3].Value.ToString();
                        if (double.Parse(label13.Text) < 0)
                        {
                            panel2.BackColor = Color.GreenYellow;
                            this.dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.GreenYellow;
                            if (label8.Text == "True")
                            {
                                panel2.BackColor = Color.RoyalBlue;
                                dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.RoyalBlue;
                                label13.ForeColor = Color.RoyalBlue;
                                DoubleBuffered = true;
                            }
                        }
                        else
                        {
                            panel2.BackColor = Color.Gainsboro;
                            if (label8.Text == "False")
                            {
                                panel2.BackColor = Color.Gainsboro;
                                dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Gold;
                                label13.ForeColor = Color.Green;
                                DoubleBuffered = true;
                            }
                        }
                    };
                    string s = strArray[5].ToString().Substring(0, strArray[5].ToString().Length - 9).Replace(@"\/", "/");
                    this.visionId = strArray[5].ToString().Substring(strArray[5].ToString().Length - 9);
                    this.visionThread = Convert.ToInt32(strArray[6]);      
                    this.label12.Text = strArray[7].ToString();                  
                    Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                    string path = "";
                    this.pictureBox1.DrawToBitmap(bitmap, this.pictureBox1.ClientRectangle);
                    this.pictureBox1.Refresh();
                    DoubleBuffered = true;
                    this.ResumeLayout(false);
                    Form1 form = (Form1)Application.OpenForms[0];
                    this.ActiveControl = textBox2;
                    TopMost = true;
                    TopLevel = true;
                    textBox2.Select();
                    form.Activate();
                    form.Focus();
                    this.showCaptcha();
                    this.timer1.Enabled = true;
                    ImageFormat bmp = null;
                    Graphics gdi = Graphics.FromImage(bitmap);
                    switch (Path.GetExtension(path))
                    {
                        case ".bmp":
                            bmp = ImageFormat.Bmp;
                            break;

                        case ".png":
                            bmp = ImageFormat.Png;
                            break;

                        case ".jpeg":
                        case ".jpg":
                            bmp = ImageFormat.Jpeg;
                            break;

                        case ".gif":
                            bmp = ImageFormat.Gif;
                            break;
                    }
                        using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(s)))
                        {
                            this.pictureBox1.Image = Image.FromStream(stream);                          
                            int clickeditem = 0;
                            clickeditem++;
                            switch (clickeditem)
                            {
                                case 1:
                                    this.pictureBox1.Image = Image.FromStream(stream);
                                    break;
                                case 2:
                                    this.pictureBox1.Image = Image.FromStream(stream);
                                    break;
                                case 3:
                                    this.pictureBox1.Image = Image.FromStream(stream);
                                    break;
                            }              
                        }
                    }
                }            
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());                
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2021-10-04
@Nipheris

And you, showing one image, immediately load the next one (maybe even a couple). It doesn't matter - from the network, or from the disk, the idea is the same.
Try scrolling in Windows Viewer FAST - you may notice a delay.
In general, since we are talking about the network, then you have a great opportunity to get acquainted with caching.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question