在开发的过程中,难免遇到图片保存问题,解决的方法有很多,这里我把图片以二进制的形式保存到数据库中,也许这个形式并不是最高效的方式,但也不失为一种好的方法吧.呵呵,下面简单的demo可以作为参考:
1![]()
Code #region Code
2
3//单击"浏览"按钮
4
5private void button1_Click(object sender, System.EventArgs e)
6
7![]()
{
8
9DialogResult result=this.openFileDialog1.ShowDialog();
10
11if(result==DialogResult.OK)
12
13![]()
{
14
15this.textBox1.Text=this.openFileDialog1.FileName.ToString
16
17();
18
19Image img = Bitmap.FromFile(this.textBox1.Text);
20
21this.pictureBox1.Image=img;
22
23}
24
25![]()
26
27}
28
29//单击"确定"按钮
30
31private void button2_Click(object sender, System.EventArgs e)
32
33![]()
{
34
35//插入数据库操作,图片类型的参数为PicToBinary()返回的byte[]即可
36
37把图片以字节的形式保存到数据库中
38
39}
40
41//图片转换为字节数组
42
43private byte[] PicToBinary()
44
45![]()
{
46
47//创建参数集
48
49string path = this.textBox1.Text.Trim();
50
51byte[] source = null;
52
53if(!path.Equals("") && File.Exists(path))
54
55![]()
{
56
57FileStream fs=new FileStream
58
59(path,FileMode.Open,FileAccess.Read);//创建文件流
60
61source=new byte[(int)fs.Length];
62
63fs.Read(source,0,(int)fs.Length);
64
65Image img = Bitmap.FromStream(fs);//把文件流转换为图片
66
67if(img.Width > 300 || img.Height > 400)
68
69![]()
{
70
71MessageBox.Show("图片过大,请上传400*300以下的图片");
72
73return;
74
75}
76
77fs.Flush();
78
79fs.Close();
80
81}
82
83return source;
84
85}
86
87![]()
88
89#endregion
转载于:https://www.cnblogs.com/yangjie5188/archive/2008/02/16/1070437.html