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