如何将asp.net中的image控件中的图片转换成字节数组 5

 我来答
匿名用户
2011-07-06
展开全部
鉴于很多同学咨询,如何向数据库中存储并读取图片的方法.在此给出代码,供大家参考.但个人建议,尽量少向数据库中存储如此大二进制数据,会造成数据库过于膨胀,如果采用DataSet读取数据时,当数据量过大还会造成内存严重占用,尤其是在B/S程序设计中,后果会更明显,除非用户强烈要求.一般存储图片时,如无特殊要求,只需要在数据库中存储图片的相对路径即可,将真正的图片存储在指定的文件夹就OK了.当然,两种方式在不同的适用面都有自己的优缺点,下面几篇文章大家可以看看:

以下是存储并显示图片的代码
private void button1_Click(object sender, EventArgs e) //button1的Click事件
//存储图片的代码,需在界面添加按钮Button1,打开文件对话框openFileDialog1,列表框listBox1和图片控件pictureBox1,代码注释部分为SqlServer实现,非注释部分为Access实现,在Access中,数据库名为mydb.mdb,表名为photos,字段为photoid(文本类型,主键),photoimg(OLE对象类型,用于存图片)
{
openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//把所选图片文件的流中的数据读入字节数组
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
conn.Open();
OleDbCommand cmd = new OleDbCommand("insert into photos values(@id,@Image)", conn);
cmd.Parameters.Add("@id", OleDbType.VarChar, 50);
cmd.Parameters.Add("@Image", OleDbType.Binary);//图片数据
cmd.Parameters["@id"].Value = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
cmd.Parameters["@Image"].Value = imagebytes;//为图片数据的SQL参数赋值
cmd.ExecuteNonQuery();
conn.Close();
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//conn.Open();
//SqlCommand cmd = new SqlCommand("insert into photos values(@id,@Image)", conn);
//cmd.Parameters.Add("@id", SqlDbType.VarChar, 50);
//cmd.Parameters.Add("@Image", SqlDbType.Image );//与ACCESS数据库唯一不同点
//cmd.Parameters["@id"].Value = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
//cmd.Parameters["@Image"].Value = imagebytes;
//cmd.ExecuteNonQuery();
//conn.Close();
MessageBox.Show("图片上传成功");
bindListBox();
}
}
private void bindListBox()//将photoId字段的数据显示在listBox1中,以便查看存入库中的每一个图片
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
OleDbCommand cmd = new OleDbCommand("select photoid from photos");
cmd.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
listBox1.DataSource = ds.Tables[0].DefaultView;//设置listBox1的数据源
listBox1.DisplayMember = "photoid";//设置listBox1即将显示数据源中的哪个字段的数据
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//SqlCommand cmd = new SqlCommand("select photoid from photos");
//cmd.Connection = conn;
//SqlDataAdapter da = new SqlDataAdapter();
//da.SelectCommand = cmd;
//DataSet ds = new DataSet();
//da.Fill(ds);
//listBox1.DataSource = ds.Tables[0].DefaultView;
//listBox1.DisplayMember = "photoid";
}
private void Form1_Load(object sender, EventArgs e) //Form1的Load事件
{
bindListBox();//程序加载时,先将数据库中现有的数据显示在listBox1中
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)//listBox1的SelectedIndexChanged事件,当在listBox1中选择不同的photoId时,pictureBox1中显示该photoId对应的图片
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
OleDbCommand cmd = new OleDbCommand("select photoimg from photos where photoid='" + ((DataRowView)(listBox1.SelectedValue))[0] + "'");//将listBox1中选中的photoId所对应的图片数据从数据库中取出来
cmd.Connection = conn;
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] b = (byte[])dr[0];//将图片数据转换成字节数组
MemoryStream stmBLOBData = new MemoryStream(b);//将该字节数组转换成内存流
pictureBox1.Image = Image.FromStream(stmBLOBData);//将该内存流转换成Image对象,并显示在pictureBox1中
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//SqlCommand cmd = new SqlCommand("select photoimg from photos where photoid='" + ((DataRowView)(listBox1.SelectedValue))[0] + "'");
//cmd.Connection = conn;
//conn.Open();
//SqlDataReader dr = cmd.ExecuteReader();
//dr.Read();
//byte[] b = (byte[])dr[0];
//MemoryStream stmBLOBData = new MemoryStream(b);
//pictureBox1.Image = Image.FromStream(stmBLOBData);
}
另外,站长团上有产品团购,便宜有保证
手机用户22921
2011-06-30 · TA获得超过264个赞
知道答主
回答量:445
采纳率:0%
帮助的人:272万
展开全部
鉴于很多同学咨询,如何向数据库中存储并读取图片的方法.在此给出代码,供大家参考.但个人建议,尽量少向数据库中存储如此大二进制数据,会造成数据库过于膨胀,如果采用DataSet读取数据时,当数据量过大还会造成内存严重占用,尤其是在B/S程序设计中,后果会更明显,除非用户强烈要求.一般存储图片时,如无特殊要求,只需要在数据库中存储图片的相对路径即可,将真正的图片存储在指定的文件夹就OK了.当然,两种方式在不同的适用面都有自己的优缺点,下面几篇文章大家可以看看:

以下是存储并显示图片的代码
private void button1_Click(object sender, EventArgs e) //button1的Click事件
//存储图片的代码,需在界面添加按钮Button1,打开文件对话框openFileDialog1,列表框listBox1和图片控件pictureBox1,代码注释部分为SqlServer实现,非注释部分为Access实现,在Access中,数据库名为mydb.mdb,表名为photos,字段为photoid(文本类型,主键),photoimg(OLE对象类型,用于存图片)
{
openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//把所选图片文件的流中的数据读入字节数组
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
conn.Open();
OleDbCommand cmd = new OleDbCommand("insert into photos values(@id,@Image)", conn);
cmd.Parameters.Add("@id", OleDbType.VarChar, 50);
cmd.Parameters.Add("@Image", OleDbType.Binary);//图片数据
cmd.Parameters["@id"].Value = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
cmd.Parameters["@Image"].Value = imagebytes;//为图片数据的SQL参数赋值
cmd.ExecuteNonQuery();
conn.Close();
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//conn.Open();
//SqlCommand cmd = new SqlCommand("insert into photos values(@id,@Image)", conn);
//cmd.Parameters.Add("@id", SqlDbType.VarChar, 50);
//cmd.Parameters.Add("@Image", SqlDbType.Image );//与ACCESS数据库唯一不同点
//cmd.Parameters["@id"].Value = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond;
//cmd.Parameters["@Image"].Value = imagebytes;
//cmd.ExecuteNonQuery();
//conn.Close();
MessageBox.Show("图片上传成功");
bindListBox();
}
}
private void bindListBox()//将photoId字段的数据显示在listBox1中,以便查看存入库中的每一个图片
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
OleDbCommand cmd = new OleDbCommand("select photoid from photos");
cmd.Connection = conn;
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
listBox1.DataSource = ds.Tables[0].DefaultView;//设置listBox1的数据源
listBox1.DisplayMember = "photoid";//设置listBox1即将显示数据源中的哪个字段的数据
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//SqlCommand cmd = new SqlCommand("select photoid from photos");
//cmd.Connection = conn;
//SqlDataAdapter da = new SqlDataAdapter();
//da.SelectCommand = cmd;
//DataSet ds = new DataSet();
//da.Fill(ds);
//listBox1.DataSource = ds.Tables[0].DefaultView;
//listBox1.DisplayMember = "photoid";
}
private void Form1_Load(object sender, EventArgs e) //Form1的Load事件
{
bindListBox();//程序加载时,先将数据库中现有的数据显示在listBox1中
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)//listBox1的SelectedIndexChanged事件,当在listBox1中选择不同的photoId时,pictureBox1中显示该photoId对应的图片
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + "[url=file://\\mydb.mdb]\\mydb.mdb[/url]");
OleDbCommand cmd = new OleDbCommand("select photoimg from photos where photoid='" + ((DataRowView)(listBox1.SelectedValue))[0] + "'");//将listBox1中选中的photoId所对应的图片数据从数据库中取出来
cmd.Connection = conn;
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
byte[] b = (byte[])dr[0];//将图片数据转换成字节数组
MemoryStream stmBLOBData = new MemoryStream(b);//将该字节数组转换成内存流
pictureBox1.Image = Image.FromStream(stmBLOBData);//将该内存流转换成Image对象,并显示在pictureBox1中
//SqlConnection conn = new SqlConnection("server=LENOVO-B2A10C83;database=mydb;uid=sa;pwd=128126");
//SqlCommand cmd = new SqlCommand("select photoimg from photos where photoid='" + ((DataRowView)(listBox1.SelectedValue))[0] + "'");
//cmd.Connection = conn;
//conn.Open();
//SqlDataReader dr = cmd.ExecuteReader();
//dr.Read();
//byte[] b = (byte[])dr[0];
//MemoryStream stmBLOBData = new MemoryStream(b);
//pictureBox1.Image = Image.FromStream(stmBLOBData);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式