winfrom存储图片

ab281021997 2010-12-29 08:19:56
用C#编写winform 存储图片到sql数据库上的代码是怎么样的 怎么在查询的时候给显示在pictruebox上啊
请各位大哥帮忙解决下啊
...全文
238 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
各位高手 帮忙解决下啊
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 piaopiao_lucky 的回复:]
wuyq11 的方法很好,强烈推荐。
[/Quote]
FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
能否解释下那个路径是什么意思
  • 打赏
  • 举报
回复
wuyq11 的方法很好,强烈推荐。
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
用这个以后怎么显示出来
iwanghs 2010-12-29
  • 打赏
  • 举报
回复

private void btnUpload_Click(object sender, EventArgs e)
{
openPic.Title = "Choose Picture";
openPic.Filter = "JPEG(*.jpg)|*.jpg|PNG(*.png)|*.png";

if (openPic.ShowDialog() == DialogResult.OK)
{
if (File.Exists(openPic.FileName))
{
if (openPic.OpenFile().Length > 1024 * 1024 * 2)
{
MessageBox.Show("图片不得超过2M");
return;
}
string image = openPic.FileName;
string picpath = openPic.FileName;
Guid guid = Guid.NewGuid();
this.txtPhoto.Text = openPic.FileName;
if (!Directory.Exists(Application.StartupPath + "\\image"))
{
Directory.CreateDirectory(Application.StartupPath + "\\image");
}
this.txtPhoto.Tag = Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg";
File.Copy(openPic.FileName, Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg");
picPhoto.Image = Image.FromFile(Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg");
picPhoto.ImageLocation = Application.StartupPath + "\\image\\" + guid.ToString() + ".jpg";
}
else
{
MessageBox.Show("图片不能为空");
return;
}
}
}


ab281021997 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
pi……
[/Quote]
FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
这里面的路径是什么用的啊 请教
wuyq11 2010-12-29
  • 打赏
  • 举报
回复
Stream ms;
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));


SqlConnection conn = new SqlConnection();
conn.ConnectionString = "";

sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn);

cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

ms.Close();
}
}

SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;


FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@image"].Value=box;
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 winnerhot 的回复:]
用bitmap 读照片。 转化成二进制流的字符串。然后储存进数据库
[/Quote]
能否给点源码给参考下
winnerhot 2010-12-29
  • 打赏
  • 举报
回复
用bitmap 读照片。 转化成二进制流的字符串。然后储存进数据库
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 adensky 的回复:]
存成二进制
然后读出来再还原为图片
[/Quote]
能给点源码参考下不 我写了但是还是出现参数错误问题啊
Adensky 2010-12-29
  • 打赏
  • 举报
回复
存成二进制
然后读出来再还原为图片
ab281021997 2010-12-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lijun84 的回复:]
存图片路径吧
[/Quote]
我也知道路径存储啊 但是存储进去的二进制数据好像是破损的 图片显示不出来
wolftop 2010-12-29
  • 打赏
  • 举报
回复
存图片路径吧
51Crack 2010-12-29
  • 打赏
  • 举报
回复
写winfrom的贴一律不理

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧