我只是想知道是否可以在没有图像路径的情况下将图像保存到数据库中,例如已分配到图片框中的默认图像。这是我使用的需要图像位置的示例代码。
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())
{
}
最佳答案
您可以使用MemoryStream
将PictureBox中的图像保存到字节数组
Image image = pictureBox.Image;
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
byte[] imageBt = memoryStream.ToArray();
其他部分是一样的。
关于c# - 如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951318/