在此代码我想使用linqtosql保存在数据库picturebox1图像的一个实例.....但得到一些例外的图像转换为字节数组System.NullReferenceException:对象不设置为一个对象
异常是“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();
}
}
几乎所有的'NullReferenceException'都是一样的。请参阅“[什么是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)”的一些提示。 – 2013-02-19 17:15:47
你在调试器中运行过吗?它会告诉你究竟哪个对象是空的。 – ChrisF 2013-02-19 17:15:50
可能是错误引发ImageToByteArray函数 – Utkarsh 2013-02-19 17:18:17