日期:2014-05-20  浏览次数:20818 次

如何将已知图像存入到数据库中,以及如何从数据库中取出图像
我是新手,还请详细一点,谢谢各位了

------解决方案--------------------
Response.BinaryWrite(company.Logo);
------解决方案--------------------
1. openFileDialog 控件 如何打开图形文件 并得到Image格式的文件

openFileDialog1.ShowDialog();
string str = openFileDialog1.FileName; //图片的路径
pictureBox1.Image = Image.FromFile(str);//图像转换Image并用PictureBox控件显示

2.如何把 pictureBox1.Image 中Image对象转换成byte[]

Image image = this.pictureBox1.Image;
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, (object)image);
ms.Close();
byte[] bytes = ms.ToArray();

3.如何把一个数据库文件Object转化成Image类型
(1) 读取数据库:
SqlConnection conn = DbHelp.RerurnConn();
SqlCommand comm = new SqlCommand("select imageico from GoodMessage where id=1", conn);
SqlDataReader read = comm.ExecuteReader();
 if (read.Read())
return read["imageico"]; //返回Object对象
 
(2) Object 转化 Image类型

byte[] bytes = (byte[])obj1; //obj1 从(1)中得到的Object 
MemoryStream ms1 = new MemoryStream(bytes, 0, bytes.Length);
BinaryFormatter bf1 = new BinaryFormatter();
object obj = bf1.Deserialize(ms1);
ms1.Close();
this.pictureBox2.Image = (Image)obj;