如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中 [英] How to save picturebox image into mysql database without imagepath

查看:155
本文介绍了如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否可以在没有图像路径的情况下将图像保存到数据库中,例如已经分配到图片框中的默认图像。这是我使用的示例代码,需要图像位置。我想在图片框中设置默认图像。 MySql中的Image列设置为Blob。感谢那些愿意回答的人。

i just wanted to know if it is possible to save images into the database without the image path like for example a default image already assigned into the picture box. This is the sample code that I used that requires an image location. I want to set a default image into the picture box. The Image column in MySql is set to Blob. Thanks to those who will answer.

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

}

推荐答案

一次又一次地回答这个问题并不好: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap + OR +位图%29& doctypeid = 4%3b5 [ ^ ]。



-SA
It would be not good to answer again and again the question asked so many times: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap+OR+bitmaps%29&doctypeid=4%3b5[^].

—SA


我找到了问题的解决方案。谢谢那些已经回答的人。 。 。

I have found the solution to my question. Thank you to those who have answered . . .
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");


这篇关于如何在没有imagepath的情况下将picturebox图像保存到mysql数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆