繁体   English   中英

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

[英]How to save picturebox image into mysql database without imagepath

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

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();

其他部分将是相同的。

我已经尝试过这个...

        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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM