我有以下代码将图像放入图片框:
OpenFileDialog f = new OpenFileDialog();
f.Filter = "JPG(*JPG)|*.jpg";
if (f.ShowDialog() == DialogResult.OK)
{
pictureBox4.Image = Image.FromFile(f.FileName);
}
以下代码将Image插入数据库:
public void Team()//insert into db new teammate
{
try
{
MemoryStream ms = new MemoryStream();
pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
byte[] a = ms.GetBuffer();
ms.Close();
SqlConnection con = new SqlConnection(stringcon); //CONNECTION
cmd.Parameters.Clear();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO team(lastname,firstname,phonenumber,email,[password],[function],[role],registerdata,personaldescription,profilepicture) VALUES(@lastname,@firstname,@phonenumber,@email,@password,@function,@role,@registerdata,@personaldescription,@profilepicture)";
cmd.Parameters.AddWithValue("@lastname", lastname_textbox.Text);
cmd.Parameters.AddWithValue("@firstname", firstname_textbox.Text);
cmd.Parameters.AddWithValue("@phonenumber", "+"+phone_textbox.Text);
cmd.Parameters.AddWithValue("@email", email_textbox.Text);
cmd.Parameters.AddWithValue("@password", repeatpassword_textbox.Text);
cmd.Parameters.AddWithValue("@function", function_textbox.Text);
cmd.Parameters.AddWithValue("@role", role_dropbox.selectedValue);
cmd.Parameters.AddWithValue("@registerdata", DateTime.Now.ToString("yyyy-MM-dd HH: mm:ss"));
cmd.Parameters.AddWithValue("@personaldescription", personaldescription_textbox.Text);
cmd.Parameters.AddWithValue("@profilepicture", a);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
ex.ToString();
return;
}
}
现在,在db中插入映像之后想要清除进程内存因为增加进程内存这个并不好,因为我想每次插入db时插入10个这样的图像。 例如:我将图像插入图片框后初始化时我有进程内存120mb我有150 mb但是当我插入10图像时我有120mb + 30mb * 10 ..但我想我可以在插入db之后清除这个内存但我不知道不知道怎么做。
答案 0 :(得分:0)
你应该去using
代码块
using 语句简化了您必须编写以创建的代码,然后最终清理该对象。 using语句获取指定的资源,执行语句并最终调用对象的Dispose方法来清理对象。
将您的代码移至using
using (SqlConnection con = new SqlConnection(connectionString))
{
MemoryStream ms = new MemoryStream();
pictureBox4.Image.Save(ms, pictureBox4.Image.RawFormat);
byte[] a = ms.GetBuffer();
ms.Close();
...........
}
场景using
后面的语句被翻译成
try
{
.....
}
finally
{
.....
}