How to free memory after performing a task?

0

On a certain task, I have to load 6 photos in 6% with%, for this I use the method below:

 private void simpleButton1_Click(object sender, EventArgs e) // botao carregar foto
    {
        OpenFileDialog carrega_foto = new OpenFileDialog();
        carrega_foto.Filter = "jpg|*.jpg";

        if (carrega_foto.ShowDialog() == DialogResult.OK)
        {
            if(pic1 == 0)
            {
                pictureBox1.ImageLocation = carrega_foto.FileName;

                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

                pic1 = 1;
            }
            else  if(pic2 == 0)
            {
                pictureBox2.ImageLocation = carrega_foto.FileName;

                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

                pic2 = 1;
            }
            else if (pic3 == 0)
            {
                pictureBox3.ImageLocation = carrega_foto.FileName;

                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;

                pic3 = 1;
            }
            else if (pic4 == 0)
            {
                pictureBox4.ImageLocation = carrega_foto.FileName;

                pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;

                pic4 = 1;
            }
            else if (pic5 == 0)
            {
                pictureBox5.ImageLocation = carrega_foto.FileName;

                pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;

                pic5 = 1;
            }
            else if (pic6 == 0)
            {
                pictureBox6.ImageLocation = carrega_foto.FileName;

                pictureBox6.SizeMode = PictureBoxSizeMode.StretchImage;

                pic6 = 1;
            }
        }
    }

Then, I take the photos, resize them, transform them into a PictureBox of array and save it to the database, using the following method:

            try
            {
                PictureBox[] List_Picture = { pictureBox1, pictureBox2, pictureBox3, pictureBox4, pictureBox5, pictureBox6 };

                foreach (PictureBox Pic in List_Picture)
                {
                    if (Pic.ImageLocation != null)
                    {
                        MemoryStream stream = new MemoryStream();

                        EncoderParameters myEncoderParameters = new EncoderParameters(1);

                        EncoderParameter myEncoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 10L);

                        myEncoderParameters.Param[0] = myEncoderParameter;

                        var codec = ObterCodec(Pic.Image.RawFormat);

                        Pic.Image.Save(stream, codec, myEncoderParameters);                            

                        byte[] Bfoto = stream.ToArray();

                        Classes.Cadastro.Crm.Analise_CRM Cad_Foto = new Classes.Cadastro.Crm.Analise_CRM();

                        Cad_Foto.Cad_Foto_Anal_Crm(textEdit8.Text, Bfoto);

                        Pic.Dispose();
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("Erro, Contate o adiministrador!" + "\n" + error, "Salvar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }     

So, by doing a byte[] , I see that when I load the photos, a lot of memory is used for that, so let's get it right ..

However, after I save the photo in the bank, how do I free up this used memory?

I tried to use the debug method but it did not work! What happens and if you try to perform this task again, the exception below is generated:

    
asked by anonymous 11.05.2017 / 03:51

2 answers

0

I was able to solve the problem with the following code:

if (pictureBox1.Image != null) 
{
   pictureBox1.Image.Dispose();
   pictureBox1.Image = null;
}

Refresh: Defining Images at Run Time (Windows Forms)

    
11.05.2017 / 19:25
0

Use the Garbage Collection:

GC.Collect();

More information:

link

    
11.05.2017 / 04:02