【问题标题】:How to save picturebox image into mysql database without imagepath如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中
【发布时间】:2014-09-20 17:15:32
【问题描述】:

我只是想知道是否可以在没有图像路径的情况下将图像保存到数据库中,例如已经分配到图片框中的默认图像。这是我使用的需要图像位置的示例代码。

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 列吗?
  • 我想设置一个图像对象是设置在图片框中的默认图像,是的,Mysql 列设置为 Blob .. 谢谢你注意到我的问题 .. xD
  • 是的,有可能。有一个不错的example video on youTube,它使用从 PictureBox.Image 提供的内存流。但是这里可能也有几个帖子,向您展示如何,例如 this one.. 看看各种 Blob 大小! Look here!! Blob 默认只有 255 字节
  • 感谢您的回答.. 非常感谢

标签: c# mysql


【解决方案1】:

您可以使用MemoryStream将图片从PictureBox保存到字节数组中

    Image image = pictureBox.Image;
    MemoryStream memoryStream = new MemoryStream();
    image.Save(memoryStream, ImageFormat.Png);

    byte[] imageBt = memoryStream.ToArray();

其他部分相同。

【讨论】:

  • 感谢您的回答.. 非常感谢
【解决方案2】:

这个我试过了……

        MemoryStream ms = new MemoryStream();
        pictureBox1.Image.Save(ms, ImageFormat.Png);
        byte[] pic_arr = new byte[ms.Length];
        ms.Position = 0;
        ms.Read(pic_arr, 0, pic_arr.Length);

        conn.Open();
        MySqlCommand cmd = new MySqlCommand("insert into images(image, name)values(@img,@imgName)", conn);
        cmd.Parameters.AddWithValue("@imgName", txtName.Text);
        cmd.Parameters.AddWithValue("@img", pic_arr);
        int n = cmd.ExecuteNonQuery();
        conn.Close();
        MessageBox.Show("COmplete");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2019-09-08
    • 2018-09-04
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多