i got out of memory exception upon loading image from file.. i retrieve the file from database and the output file image can be viewed..
String temp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/img.jpg";
using (FileStream fs = new FileStream(temp, FileMode.Create))
{
fs.Write(StudentImage(id), 0, StudentImage(id).Length);
开发者_如何学运维 fParent.picAvatar.BackgroundImage = Image.FromFile(temp);
//fParent.picAvatar.Image = Image.FromFile(temp2);
}
private byte[] StudentImage(String _id)
{
try
{
String sqlCmd = String.Format("SELECT studpic FROM dbo.studentpic WHERE idnum = '{0}'", _id);
using (SqlConnection con = new SqlConnection(gVar._conString))
{
con.Open();
SqlCommand cmd = new SqlCommand(sqlCmd, con);
using (SqlDataReader r = cmd.ExecuteReader())
{
r.Read();
byte[] imgData = (byte[])r["studpic"];
return imgData;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
did i miss any object that needs to close?
You tend to get an OutOfMemory exception when an image cannot be read for a number of reasons.
In your case the issue is that you have not closed your file! Also, you are calling the database twice.
I suggest the following code:
File.WriteAll(temp, StudentImage(id));
fParent.picAvatar.Image = Image.FromFile(temp);
精彩评论