我只是想知道是否可以将图像保存到数据库中,而不需要图像路径,例如已经分配给图片框的默认图像。这是我使用的需要图像位置的示例代码。如何在没有图像路径的情况下将图片箱图像保存到mysql数据库中
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txtImage.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
conn.Open();
MySqlCommand command = new MySqlCommand("insert into images(image)values(@IMG)", conn);
command.Parameters.Add(new MySqlParameter("@IMG", imageBt));
reader = command.ExecuteReader();
MessageBox.Show("Saved");
while (reader.Read())
{
}
不然怎么想接收图像作为?一个图像或位图对象?问题中的Mysql列是Blob列吗? – ChargerIIC 2014-09-20 17:26:28
我想设置一个图像对象是一个默认图像设置到图片框和是的MySQL列设置为Blob ..谢谢你注意到我的问题.. xD – 2014-09-20 17:29:23
是的,这是可能的。有一个不错的[youTube视频示例](https://www.youtube.com/watch?v=szIj1xvNfMU),它使用从PictureBox.Image提供的内存流。但这里也可能有几个帖子,告诉你如何,像[这一个](http://stackoverflow.com/questions/20503154/load-an-image-to-c-sharp-and-then-inserting- into-mysql-table)..请看看各种Blob大小! [看这里](http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-save-images-in-mysql-database-using-C-Sharp/)! Blob只有255字节,默认情况下 – TaW 2014-09-20 18:02:44