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;
}
}web