C# winForm怎么把pictureBox显示的图片放到数据库中和从数据库中读取出来显示到pictureBox中
发布网友
发布时间:2022-04-08 02:47
我来回答
共3个回答
热心网友
时间:2022-04-08 04:16
你最好用 地址 放图片。 二进制的话给你
这是上传图片 代码:
Stream aa;
OpenFileDialog opg = new OpenFileDialog();
if (opg.ShowDialog() == DialogResult.OK)
{
pictureBox2.Load(opg.FileName);//获取要上传的图片
aa= opg.OpenFile();
}
string con = "data source=.;uid=sa;pwd=;database=master";
SqlConnection conn = new SqlConnection(con);
byte[] bytes = new byte[aa.Length];
aa.Read(bytes, 0, (int)aa.Length);
conn.Open();
string sql1 = string.Format("insert into 数据库表格 values('{0}',@phone)", 用户名);
SqlCommand cmd1 = new SqlCommand(sql1, conn);
cmd1.Parameters.Add("@phone", SqlDbType.Image).Value = bytes;
cmd1.ExecuteNonQuery();
conn.Close();
MessageBox.Show( "用户名的图片已上传");
从数据库得到二进制图片,转化为pictrue:
string sql = "select 图片 from 数据库表格 where name=用户名";
SqlConnection conn = new SqlConnection("data source=.;uid=sa;pwd=****;database=master");
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
byte[] bs = (byte[])r["图片"];
MemoryStream ms = new MemoryStream(bs, true);
ms.Write(bs, 0, bs.Length);
pictureBox1.Image = new Bitmap(ms, true);
ms.Dispose();
conn.close();
最重要要能理解代码,懂了就简单了。 希望采纳。
热心网友
时间:2022-04-08 05:34
没有存图片名,灵活
热心网友
时间:2022-04-08 07:09
//保存图片:
SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into image values(@i)", conn);
byte[] ib = new byte[60000];
FileStream fs = new FileStream(this.openFileDialog1.FileName.ToString(), FileMode.Open, FileAccess.Read);
fs.Read(ib, 0, 60000);
cmd.Parameters.Add("@i", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@i"].Value = ib;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("保存成功");
//显示图片:
SqlConnection conn = new SqlConnection(@"data source=.;uid=sa;pwd=;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("select image1 from image", conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
MemoryStream buf = new MemoryStream((byte[])reader[i]);
Image image = Image.FromStream(buf,true);
this.pictureBox1.Image = image;
}
}