WPF用流的方式上传/显示/下载图片文件(保存在数据库)

前两天有同事问我怎么将图片存入数据库,即兴写了个列子.

1.在数据库中,图片字段用varbinary(MAX)类型.

程序代码(下附完整的例子下载):

1 using System;
2  using System.Linq;
3  using System.Windows;
4  using System.Windows.Media.Imaging;
5 using System.IO;
6 using System.Data.Linq;
7
8 namespace WpfUploadDispalyIMG
9 {
10
11 public partial class MainWindow : Window
12 {
13 DataClasses1DataContext db = new DataClasses1DataContext();
14 public MainWindow()
15 {
16 InitializeComponent();
17 }
18
19 private void btBrowse_Click(object sender, RoutedEventArgs e)
20 {
21 //创建"打开文件"对话框
22 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
23
24 //设置文件类型过滤
25 dlg.Filter = "图片|*.jpg;*.png;*.gif;*.bmp;*.jpeg";
26
27 // 调用ShowDialog方法显示"打开文件"对话框
28 Nullable<bool> result = dlg.ShowDialog();
29
30 if (result == true)
31 {
32 //获取所选文件名并在FileNameTextBox中显示完整路径
33 string filename = dlg.FileName;
34 FileNameTextBox.Text = filename;
35
36 //在image1中预览所选图片
37 BitmapImage image = new BitmapImage(new Uri(filename));
38 image1.Source = image;
39 image1.Width = image.Width;
40 image1.Height = image.Height;
41 }
42 }
43
44 private void btUpdate_Click(object sender, RoutedEventArgs e)
45 {
46 if (!string.IsNullOrEmpty(FileNameTextBox.Text))
47 {
48 UploadIMG();
49 MessageBox.Show("OK!");
50 FileNameTextBox.Text = string.Empty;
51
52 }
53 else
54 MessageBox.Show("请选择图片!");
55 }
56
57
58 //上传图片至数据库
59 private void UploadIMG()
60 {
61 //将所选文件的读入字节数组img
62 byte[] img = File.ReadAllBytes(FileNameTextBox.Text);
63 string fileName = System.IO.Path.GetFileNameWithoutExtension(FileNameTextBox.Text);
64 //FileNameTextBox.Text.Substring(FileNameTextBox.Text.LastIndexOf('\\')+1);
65 Crew newCrew = new Crew()
66 {
67 photo = img,//将图片写入数据库
68 name = fileName
69 };
70 db.Crew.InsertOnSubmit(newCrew);
71 db.SubmitChanges();
72 }
73
74 private void btShow_Click(object sender, RoutedEventArgs e)
75 {
76 try
77 {
78 //根据文件名读取二进制形式的图片
79 Binary p = db.Crew.FirstOrDefault(c => c.name == tbName.Text.Trim()).photo;
80 byte[] img = p.ToArray();
81 ShowSelectedIMG(img);
82 }
83 catch (Exception ex)
84 {
85 MessageBox.Show(ex.Message);
86 }
87 }
88 //显示图片
89 private void ShowSelectedIMG(byte[] img)
90 {
91 BitmapImage image = new BitmapImage();
92 //以流的形式初始化图片
93 image.BeginInit();
94 image.StreamSource = new MemoryStream(img);
95 image.EndInit();
96
97 image1.Source = image;
98 image1.Width = image.PixelWidth;
99 image1.Height = image.PixelHeight;
100 }
101 //保存为文件
102 private void btSave_Click(object sender, RoutedEventArgs e)
103 {
104 //创建"保存文件"对话框
105
106 Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
107 //设置默认文件类型
108 dlg.DefaultExt = ".png";
109 Nullable<bool> result = dlg.ShowDialog();
110
111 if (result == true)
112 {
113 //获取要保存文件名的完整路径
114 string filename = dlg.FileName;
115 //将文件流写入文件
116 FileStream write = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, System.IO.FileShare.Read);
117 Binary p = db.Crew.FirstOrDefault(c => c.name == tbName.Text.Trim()).photo;
118 byte[] img = p.ToArray();
119 write.Write(img, 0, img.Count());
120 write.Close();
121 MessageBox.Show("成功保存");
122 }
123 }
124 }
125 }

例子下载地址: https://files.cnblogs.com/Laro/WpfUploadDispalyIMG.rar

posted @ 2011-05-23 08:45  Laro  阅读(8854)  评论(2编辑  收藏  举报