WPF实现的图片保存显示有些不一样,有必要自我总结一下。。。
【注:数据库中保存图片的数据类型最好是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;
        }
哈哈,拥有以上这几步,图片预览、保存、显示基本就可实现了!!!(有兴趣的可以试试哦!有啥问题相互交流哦!)