winform 上传图片(留作学习资料)
2009-10-28 02:33
176 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
namespace ReadImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Filter = "图像文件(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
if (Dialog.ShowDialog() == DialogResult.OK)
{
string FileName = Dialog.FileName;
FileStream fs = new FileStream(FileName, FileMode.Open);
byte[] b=new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
b = br.ReadBytes(Convert.ToInt32(fs.Length));
string sql = "INSERT INTO [Mag_Rol].[dbo].[ImageTable] VALUES(4,@photo)";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@photo",b)
};
Insert(sql, param);
string select = "select * from [ImageTable] where imageID=@imageID";
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@imageID",4)
};
SqlDataReader dr = GetData(select, parameter);
dr.Read();
if (dr[1] != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dr[1]);
Image image = Image.FromStream(ms);
this.pictureBox1.Image = image;
}
else
{
this.pictureBox1.Image = null;
MessageBox.Show("Not Sucessed");
}
conn.Close();
dr.Dispose();
}
}
private static string connectionstring = "server=.;database=Mag_Rol;Integrated Security=sspi";
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm;
public void Insert(string sql,SqlParameter[] param)
{
try
{
conn.Open();
comm = new SqlCommand(sql, conn);
foreach (SqlParameter pa in param)
{
if (pa != null)
{
comm.Parameters.Add(pa);
}
}
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
comm.Dispose();
}
}
public SqlDataReader GetData(string sql,SqlParameter[] param)
{
try
{
conn.Open();
comm = new SqlCommand(sql, conn);
foreach (SqlParameter pa in param)
{
if (pa != null)
{
comm.Parameters.Add(pa);
}
}
SqlDataReader read = comm.ExecuteReader();
return read;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
namespace ReadImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Filter = "图像文件(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
if (Dialog.ShowDialog() == DialogResult.OK)
{
string FileName = Dialog.FileName;
FileStream fs = new FileStream(FileName, FileMode.Open);
byte[] b=new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
b = br.ReadBytes(Convert.ToInt32(fs.Length));
string sql = "INSERT INTO [Mag_Rol].[dbo].[ImageTable] VALUES(4,@photo)";
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("@photo",b)
};
Insert(sql, param);
string select = "select * from [ImageTable] where imageID=@imageID";
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@imageID",4)
};
SqlDataReader dr = GetData(select, parameter);
dr.Read();
if (dr[1] != DBNull.Value)
{
MemoryStream ms = new MemoryStream((byte[])dr[1]);
Image image = Image.FromStream(ms);
this.pictureBox1.Image = image;
}
else
{
this.pictureBox1.Image = null;
MessageBox.Show("Not Sucessed");
}
conn.Close();
dr.Dispose();
}
}
private static string connectionstring = "server=.;database=Mag_Rol;Integrated Security=sspi";
SqlConnection conn = new SqlConnection(connectionstring);
SqlCommand comm;
public void Insert(string sql,SqlParameter[] param)
{
try
{
conn.Open();
comm = new SqlCommand(sql, conn);
foreach (SqlParameter pa in param)
{
if (pa != null)
{
comm.Parameters.Add(pa);
}
}
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
conn.Close();
comm.Dispose();
}
}
public SqlDataReader GetData(string sql,SqlParameter[] param)
{
try
{
conn.Open();
comm = new SqlCommand(sql, conn);
foreach (SqlParameter pa in param)
{
if (pa != null)
{
comm.Parameters.Add(pa);
}
}
SqlDataReader read = comm.ExecuteReader();
return read;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
相关文章推荐
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- C# + WinForm + EmguCV 学习二:创建和操作图片
- 学习笔记:PHP上传图片代码详解
- weex学习之路(二)---组件封装(3)--图片上传
- Java学习日记8:文件上传工具类的实现和服务器给客户端发送图片
- 前端图片预览,上传前预览,兼容IE7、8、9、10、11,Firefox,Chrome(学习到的知识)
- PHP学习笔记(十二):$_FILES超级全局变量(借助网上社区项目的上传图片功能进行介绍)
- Winform客户端到Java服务端的图片展示和上传
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.10.SportsStore:上传图片
- 1- 开始学习 context-M3 LM3S1138 上传timer 的学习资料
- iOS学习:调用相机,选择图片上传,带预览功能
- JAVA学习笔记_使用Servlet3.0上传图片,无法使用part.getSubmittedFileName()方法解决
- Java学习之TCP上传图片
- 黑马程序员之WinForm编程基础学习笔记:页面上有一张图片,默认是隐藏的,用户在文本框中输入身份证号(131226198105223452),点击按钮,如果年龄大于18岁则显示图片。
- FormData实现上传多图片,学习使用FormData
- 【微信开发学习笔记(二)】:上传图片后显示不了
- phpcms学习总结-图片上传不成功
- nodeJs学习过程之一个图片上传显示的例子
- 【SpringMVC注解开发】上传图片---json数据交互学习笔记十
- 个人资料上传头像模块,拍照+图库+图片剪裁+圆形头像