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


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

    .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.             }
  • 相关阅读:
    Codeforces 735C:Tennis Championship(数学+贪心)
    HDU 5934:Bomb(强连通缩点)
    BZOJ-2743 采花
    BZOJ-1878 HH的项链
    BZOJ-1798 维护序列
    BZOJ-1911 特别行动队
    BZOJ-1010 玩具装箱
    BZOJ-1096 仓库建设
    BZOJ-1012 最大数
    ZOJ 3696 Alien's Organ(泊松定理,期望值)
  • 原文地址:https://www.cnblogs.com/xpxu/p/1703868.html
Copyright © 2020-2023  润新知