在这段代码中,我想使用 linqtosql 将图片保存在数据库中的 picturebox1 中......但是在将图像转换为字节数组时遇到了一些异常
异常是“System.NullReferenceException:对象引用未设置为对象的实例。”
private void button1_Click(object sender, EventArgs e)
{
DataClasses1DataContext dc = new DataClasses1DataContext();
try
{
string signname = textBox1.Text;
string imageurl = textBox2.Text;
pictureBox1.ImageLocation = imageurl;
// byte[] file_byte = new byte[1000];
// Image newimage = new Image(pictureBox1.Image);
///Error comes here
byte[] file_byte = ImageToByteArray(pictureBox1.Image);
System.Data.Linq.Binary file_binary = new System.Data.Linq.Binary(file_byte);
Sign_Table obj = new Sign_Table()
{
Sign_Name = signname,
Sign_Image = file_binary,
};
dc.Sign_Tables.InsertOnSubmit(obj);
}
finally
{
dc.SubmitChanges();
}
}
private byte[] ImageToByteArray(Image imageIn )
{
using (MemoryStream ms = new MemoryStream())
{
// Error comes here
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}