C# winform 上传图片 显示图片
2012-04-28 16:31
246 查看
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。http://long7s.blog.51cto.com/2889364/587090
最近正在用C#做一个小的程序,其中用涉及到了照片的存取与显示,在网上搜索了很多有关的代码,但是几乎没有完整,大部分只是其中的存取或者显示代码,笔者将其整理了一下,形成了一个集照片上传到数据库、照片显示于一体的小模块。
原始出处 、作者信息和本声明。否则将追究法律责任。http://long7s.blog.51cto.com/2889364/587090
最近正在用C#做一个小的程序,其中用涉及到了照片的存取与显示,在网上搜索了很多有关的代码,但是几乎没有完整,大部分只是其中的存取或者显示代码,笔者将其整理了一下,形成了一个集照片上传到数据库、照片显示于一体的小模块。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //选择按钮事件处理 private void button1_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = "C:\\"; openFileDialog1.Filter = "图片文件 (*.jpg)|*.jpg"; openFileDialog1.FilterIndex = 1; openFileDialog1.RestoreDirectory = true; openFileDialog1.Multiselect = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } //上传按钮事件处理 private void button2_Click(object sender, EventArgs e) { Save(PhotoToArray(textBox1.Text.ToString())); } //将图片信息转换成二进制信息 private byte[] PhotoToArray(string path) { FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bufferPhoto = new byte[stream.Length]; stream.Read(bufferPhoto, 0, Convert.ToInt32(stream.Length)); stream.Flush(); stream.Close(); return bufferPhoto; } //把二进制的图片插到数据库 private void Save(byte[] image) { string sql = "insert into Photo(photo_Info) values(@photo)"; SqlParameter param = new SqlParameter(); param = new SqlParameter("@photo", SqlDbType.Image); param.Value = image; SqlCommand commd = new SqlCommand(sql, DBhelper.con); commd.Parameters.Add(param); try { DBhelper.con.Open(); commd.ExecuteNonQuery(); MessageBox.Show("您已经把图片成功的插入数据库!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBhelper.con.Close(); } } //显示图片按钮事件处理 private void button3_Click(object sender, EventArgs e) { string strSQL = "Select [photo_Info] From [Photo] Where [photo_Id]=(@photo)"; SqlParameter param = new SqlParameter(); param = new SqlParameter("@photo", SqlDbType.Int); param.Value = comboBox1.Text.ToString(); SqlCommand cmd = new SqlCommand(strSQL, DBhelper.con); cmd.Parameters.Add(param); DBhelper.con.Open(); System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader(); try { reader.Read(); MemoryStream ms = new MemoryStream((byte[])reader["photo_Info"]); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); this.pictureBox1.Image = image; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBhelper.con.Close(); } } private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“myPhotoDataSet.Photo”中。您可以根据需要移动或移除它。 this.photoTableAdapter.Fill(this.myPhotoDataSet.Photo); } } }
相关文章推荐
- C# winform 上传图片,显示图片和gridview中添加按钮
- struts2图片上传并且显示(注意乱码)
- springmvc上传图片并显示--支持多图片上传
- 数据包上传到shopex商城后图片显示有问题的解决方法
- MVC应用程序显示上传的图片
- thinkphp结合KindEditor 上传图片的时候出现“\”导致图片不能正常显示,
- asp.net mvc 2.0 简单的上传图片到数据库和显示图片
- 上传base64图片无法显示
- SSH框架搭建电商系统之图片上传与显示
- 利用SmartUpload+servlet上传图片文字,上传显示图片,解决乱码问题
- php上传图片到mysql并显示
- struts2上传图片,并显示在 jsp 上
- 【ASP.NET 进阶】图片自动上传和局部刷新显示
- 上传图片至数据库再显示方法(MSSQL和MYSQL)
- bootstrap-3-上传图片-列表显示
- Spring MVC 上传、下载、显示图片
- 图片上传直接显示
- 上传图片然后显示在JSP页面上
- 图片上传IE11上传成功但图片无法显示问题的解决方案
- file上传图片 显示点选的上传图片(并没有上传到服务器之前)