c# - 如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中

标签 c# 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())
{

}

最佳答案

您可以使用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/

相关文章:

java - 配置单元 Metastore 和连接拒绝进入配置单元外壳的错误

mysql - 将数据发送到 REST api 的良好实现?

c# - LeftMouseUp 未触发 - Bing map MapPolyline

c# - 如何在 C# 中创建自定义文件扩展名?

mysql - 如何将Windows 2008 R2上的Mysql数据库备份到Amazon S3?

php - MySQL SELECT 语句不接受两个 php 变量

php - 我如何通过php从mysql数据库中获取图像

c# - JSON.NET 序列化在 ApiController 中的行为不同

c# - 在没有 MVC 的纯 ASP.NET Web API 服务中,我们是否仍然需要 Global.asax 中的 AreaRegistration.RegisterAllAreas()

c# - 与泛型的反/协方差 - 不能分配给 T