C# winForm怎么把pictureBox显示的图片放到数据库中和从数据库中读取出来显示到pictureBox中
我设计的是个人信息中更换图片的功能,我不要存储路径的。选择图片功能我是这样写的//更换图片按钮privatevoidbutton1_Click(objectsender,...
我设计的是个人信息中更换图片的功能,我不要存储路径的。选择图片功能我是这样写的
//更换图片 按钮
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f1 = new OpenFileDialog();
if (f1.ShowDialog() == DialogResult.OK)
{
try
{
this.pictureBox1.BackgroundImage = Image.FromFile(f1.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
然后怎么把图片转换成二进制放到数据库中?
再怎么把图片从数据库中取出来放到pictureBox中?
要详细步骤、有注释,我对这个一点也不懂!请高手帮我,急用!谢谢!
我用的是SQL Server 2005 展开
//更换图片 按钮
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f1 = new OpenFileDialog();
if (f1.ShowDialog() == DialogResult.OK)
{
try
{
this.pictureBox1.BackgroundImage = Image.FromFile(f1.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
然后怎么把图片转换成二进制放到数据库中?
再怎么把图片从数据库中取出来放到pictureBox中?
要详细步骤、有注释,我对这个一点也不懂!请高手帮我,急用!谢谢!
我用的是SQL Server 2005 展开
3个回答
展开全部
你最好用 地址 放图片。 二进制的话给你
这是上传图片 代码:
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();
最重要要能理解代码,懂了就简单了。 希望采纳。
这是上传图片 代码:
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();
最重要要能理解代码,懂了就简单了。 希望采纳。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有存图片名,灵活
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//保存图片:
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询