0
In a certain task, I have to upload 6 photos in 6 PictureBox
, 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;
}
}
}
So I take the photos, resize them, transform them into one array
of byte[]
and saved in the database, with the method below:
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);
}
then performing a debug
I saw that when I upload the photos, is used enough memory for that, so far so good..
However, after I save the photo in the bank, how do I release this memory used?
tried to use the method Dispose
but it didn’t work!
What happens and that if I try to perform this task again, the exception below is generated:
That’s basically it: https://answall.com/q/163768/101. This code is creating several resources that are never released. You have to see the documentation of all objects used to know which ones need to be released, or use some tool to indicate this to you, such as Resharper. In fact this code has several other small problems.
– Maniero
@bigown correct me if I’m wrong, so what I can do is, use the method
using
anddispose
and wait for the "garbage collection" system?– Thomas Erich Pimentel
The
using
, theDispose()
how you used will not run if there is an exception.– Maniero