public static class FileHelper
{
//输入图片路径,得到图片的二进制数据
/// <summary>
/// 输入图片路径,得到图片的 Byte[] 二进制数据
/// </summary>
/// <param name="p_w_picpathpath"></param>
/// <returns></returns>
public static byte[] GetPictureData(string p_w_picpathpath)
{
//文件流打开文件 {1,0,0,0,0,0}
FileStream fs = new FileStream(p_w_picpathpath, FileMode.Open);
//定义数组 ,长度为文件流的长度
byte[] byteData = new byte[fs.Length];
fs.Read(byteData, 0, byteData.Length);
fs.Close();
return byteData;
}
/// <summary>
/// 根据 byte[] 数组,还原为图片对象
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static Image ReturnPhoto(byte[] streamByte)
{
MemoryStream ms = new MemoryStream(streamByte);
Image img = Image.FromStream(ms);
return img;
}
/// <summary>
/// 写图片 传入图片二进制信息,和图片地址
/// </summary>
/// <param name="streamByte"></param>
/// <param name="photoUrl"></param>
/// <returns></returns>
public static bool ReturnPhoto(byte[] streamByte, string photoUrl)
{
try
{
MemoryStream ms = new MemoryStream(streamByte);
Image img = Image.FromStream(ms);
//文件流打开文件 {1,0,0,0,0,0}
FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate);
fs.Write(streamByte, 0, streamByte.Length);
fs.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 传入 Image 对象,返回 Byte[] 类型
/// </summary>
/// <param name="imgPhoto"></param>
/// <returns></returns>
public static byte[] GetPictureData(System.Drawing.Image imgPhoto)
{
MemoryStream mstream = new MemoryStream();
imgPhoto.Save(mstream, ImageFormat.Bmp);
byte[] byteData = new byte[mstream.Length];
mstream.Position = 0;
mstream.Read(byteData, 0, byteData.Length);
mstream.Close();
return byteData;
}
}
转载于:https://blog.51cto.com/nxybcce/1279702