.NET二进制图片存储与读取

.NET二进制图片存储:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].
   
    1.参数是图片路径:返回Byte[]类型:
   
    1.public byte[] GetPictureData(string imagepath)
   
    2. {
   
    3. //根据图片文件的路径使用文件流打开,并保存为byte[]
   
    4. FileStream fs = new FileStream(imagepath, FileMode.Open);
   
    5. byte[] byData = new byte[fs.Length];
   
    6. fs.Read(byData, 0, byData.Length);
   
    7. fs.Close();
   
    8. return byData;
   
    9. }
   
    10.
   
    2.参数类型是Image对象,返回Byte[]类型:
   
    1.public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
   
    2. {
   
    3. //将Image转换成流数据,并保存为byte[]
   
    4. MemoryStream mstream = new MemoryStream();
   
    5. imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
   
    6. byte[] byData = new Byte[mstream.Length];
   
    7. mstream.Position = 0;
   
    8. mstream.Read(byData, 0, byData.Length);
   
    9. mstream.Close();
   
    10. return byData;
   
    11. }
   

    好了,这样通过上面的方法就可以把图片转换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把图片的二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的图片读取出来,实际上这是一个相反的过程。
   
    .NET二进制图片读取:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX
   
    1.参数是Byte[]类型,返回值是Image对象:
   
    1.public System.Drawing.Image ReturnPhoto(byte[] streamByte)
   
    2. {
   
    3. System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
   
    4. System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
   
    5. return img;
   
    6. }
   
    2.参数是Byte[] 类型,没有返回值,这是针对asp.net中把图片从输出到网页上(Response.BinaryWrite)
   
    1.public void WritePhoto(byte[] streamByte)
   
    2. {
   
    3. // Response.ContentType 的默认值为默认值为"text/html"
   
    4. Response.ContentType = "image/GIF";
   
    5. //图片输出的类型有: image/GIF image/JPEG
   
    6. Response.BinaryWrite(streamByte);
   
    7. }
   
    补充:
   
    针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型:
   
    1.Response.ContentType = "application/msword";
   
    2. Response.ContentType = "application/x-shockwave-flash";
   
    3. Response.ContentType = "application/vnd.ms-excel";
   
    另外可以针对不同的格式,用不同的输出类型以适合不同的类型:
   
    1.switch (dataread("document_type"))
   
    2. {
   
    3. case "doc":
   
    4. Response.ContentType = "application/msword";
   
    5. case "swf":
   
    6. Response.ContentType = "application/x-shockwave-flash";
   
    7. case "xls":
   
    8. Response.ContentType = "application/vnd.ms-excel";
   
    9. case "gif":
   
    10. Response.ContentType = "image/gif";
   
    11. case "Jpg":
   
    12. Response.ContentType = "image/jpeg";
   
    13. }
   
    protected void Button1_Click(object sender, EventArgs e)
   
    {
   
    if (FileUpload1.HasFile)
   
    {
   
    Stream mystream = FileUpload1.PostedFile.InputStream;//FileUpload1是上传控件
   
    int length = FileUpload1.PostedFile.ContentLength;
   
    byte[] bytes = new byte[length];
   
    mystream.Read(bytes, 0, length);
   
    mystream.Close();
   
    string conn = "server=(local);database=Image;Uid=sa;Pwd=123456 ";
   
    SqlConnection myconn = new SqlConnection(conn);
   
    myconn.Open();
   
    string str= "insert into image (img) values( ' " + bytes + " ') ";
   
    SqlCommand mycomm = new SqlCommand(str, myconn);
   
    mycomm.ExecuteNonQuery();
   
    myconn.Close();
   
    }
   
    }

   
    上面把图片存入数据库…
   
    下面是把图片从数据库中读出来…
   
    protected void Button2_Click(object sender, EventArgs e)
   
    {
   
    string imgtype = FileUpload1.PostedFile.ContentType;
   
    string conn = "server=(local);database=Image;Uid=sa;Pwd=123456 ";
   
    string str= "select img from image where ID=5 ";
   
    SqlConnection myconn = new SqlConnection(conn);
   
    myconn.Open();
   
    //SqlDataAdapter myda = new SqlDataAdapter(str, conn);
   
    //DataSet myds =new DataSet();
   
    //myda.Fill(myds);
   
    SqlCommand mycom = new SqlCommand(str, myconn);
   
    SqlDataReader mydr = mycom.ExecuteReader();
   
    if (mydr.Read())
   
    {
   
    Response.ContentType = imgtype;
   
    Response.BinaryWrite((byte[])mydr[ "img "]);
   
    }
   
    else
   
    {
   
    Response.Write( "没有从数据库中读取图片 ");
   
    }
   
    myconn.Close();
   
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值