C#中将图片文件转化为二进制数组-用于数据库存储

在项目开发中,使用SQL Server存储数据,数据类型image可以保存图片。但是在存储之前需要将图片转化为二进制数组的形式进行赋值。

将图片文件转换为二进制数组

/// <summary>   
/// 将图片文件转换为二进制数组   
/// </summary>   
/// <param name=”picpath”>图片路径</param>   
/// <returns>二进制数组</returns>   
private byte[] GetBinaryData(String picpath)//将图片文件转换为byte[]   

{   

    //只读方式打开图片到流   

    FileStream fs = new FileStream(picpath, FileMode.Open, FileAccess.Read);   

    //初始化一个长度正好的二进制数组imageBytes   

    Byte[] imageBytes = new byte[fs.Length];   

    //从流中读取全文,并写入二进制数组imageBytes中   

    fs.Read(imageBytes, 0, Convert.ToInt32(fs.Length));   

    //记得释放流   

    fs.Flush();   

    fs.Close();   

    //返回imageBytes   

    return imageBytes;   

}  
原文地址:https://www.cnblogs.com/huhangfei/p/5000746.html