WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
2012-11-09 15:07
645 查看
WPF实现的图片保存显示有些不一样,有必要自我总结一下。。。
【注:数据库中保存图片的数据类型最好是varbiary(max)】
1.图片预览功能:
2.以流的方式将图片保存在数据库
3.从数据库中读取以流的方式显示
哈哈,拥有以上这几步,图片预览、保存、显示基本就可实现了!!!(有兴趣的可以试试哦!有啥问题相互交流哦!)
本文来自wy926的博客,原文地址:http://www.cnblogs.com/wyuan/archive/2012/05/22/2513103.html
【注:数据库中保存图片的数据类型最好是varbiary(max)】
1.图片预览功能:
private void btn_preview_Click(object sender, RoutedEventArgs e) { OpenFileDialog openfiledialog = new OpenFileDialog(); openfiledialog.Filter = "图片(*.jpg;*.png;*.gif;*.bmp;*.jpeg)|*.jpg;*.png;*.gif;*.bmp;*.jpeg"; if ((bool)openfiledialog.ShowDialog()) { BitmapImage bitmapimg = new BitmapImage(new Uri(openfiledialog.FileName)); image1.Source = bitmapimg; //image1.Width = bitmapimg.Width; //image1.Height = bitmapimg.Height; this.tb_selPic.Text = openfiledialog.FileName; } }
2.以流的方式将图片保存在数据库
private void btn_add_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(this.tb_selPic.Text)) { byte[] img = File.ReadAllBytes(this.tb_selPic.Text); //将图片装换为字节,存储在数据库中 MyJobs myJobs = new MyJobs { Job_desc = this.tb_jobdesc.Text, Pic = img, }; List<SqlParameter> sqlPar = new List<SqlParameter> { new SqlParameter("@job_desc",myJobs.Job_desc), new SqlParameter("@pic",myJobs.Pic), }; string msg = DBHelper.DBSQLHelper.Execute("proc_myjobs", sqlPar, System.Data.CommandType.StoredProcedure) > 0 ? "添加成功!":"添加失败!"; MessageBox.Show(msg); } else { MessageBox.Show("请选择图片!"); } }
3.从数据库中读取以流的方式显示
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if ((e.OriginalSource as DataGrid).SelectedItem != null) { int job_id = Convert.ToInt32(((e.OriginalSource as DataGrid).SelectedItem as DataRowView)[0].ToString()); DataSet ds = DBHelper.DBSQLHelper.Search("select * from myjobs where job_id=" + job_id, null, CommandType.Text); this.tb_jobdesc.Text = ds.Tables[0].Rows[0]["job_desc"].ToString(); byte[] img = (byte[])ds.Tables[0].Rows[0]["pic"]; //从数据库中获取图片数据转换为字节数组(注意:不用用这种方式转换为字节数组,这种转换有问题,我之前一直出不来效果 byte[] img = System.Text.ASCIIEncoding.ASCII.GetBytes(ds.Tables[0].Rows[0]["pic"].ToString()); 现在修改了,就能出来效果了,这个问题还挺让人纠结的呢,所以大家要注意哦!) ShowSelectedIMG(img); //以流的方式显示图片的方法 } } private void ShowSelectedIMG(byte[] img) { System.IO.MemoryStream ms = new System.IO.MemoryStream(img);//img是从数据库中读取出来的字节数组 ms.Seek(0, System.IO.SeekOrigin.Begin); BitmapImage newBitmapImage = new BitmapImage(); newBitmapImage.BeginInit(); newBitmapImage.StreamSource = ms; newBitmapImage.EndInit(); image1.Source = newBitmapImage; }
哈哈,拥有以上这几步,图片预览、保存、显示基本就可实现了!!!(有兴趣的可以试试哦!有啥问题相互交流哦!)
本文来自wy926的博客,原文地址:http://www.cnblogs.com/wyuan/archive/2012/05/22/2513103.html
相关文章推荐
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- wpf图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 图片保存到数据库和从数据库读取图片并显示
- 如何将pictureBox里的图片保存到数据库,然后从数据读取显示
- struts保存图片到数据库 ,从数据库中读取图片显示到页面
- 加密字符串与图片保存于数据库,并显示读取显示
- 如何读取保存在数据库中的图片显示在DataGrid中
- WPF中把图片保存到数据库,并且从数据库提取出来显示
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 如何读取保存在数据库中的图片显示在DataGrid中
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 将图片以二进制的方式保存在数据库中,并显示图片
- C#保存图片到数据库,读取图片显示
- C#保存图片到数据库并读取显示图片的方法