/// <summary>
/// byte数组转换为Image对象
/// </summary>
/// <param name="buffer">byte数组</param>
/// <returns>Image对象</returns>
public static System.Drawing.Image BytesToImage(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
return image;
}
/// <summary>
/// 从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上
/// </summary>
/// <param name="buffer">byte数组</param>
/// <returns></returns>
public static bool CreateImageFromBytes(string path, byte[] buffer,out string returnMsg,out string fileName)
{
fileName = "";
returnMsg = "";
string file = DateTime.Now.ToString("yyyyMMddHHmmss").ToString();
System.Drawing.Image image = BytesToImage(buffer);
ImageFormat format = image.RawFormat;
if (format.Equals(ImageFormat.Jpeg))
{
file += ".jpg";
}
else if (format.Equals(ImageFormat.Gif))
{
file += ".gif";
}
else if (format.Equals(ImageFormat.Bmp))
{
file += ".bmp";
}
else
{
returnMsg = "图片格式不符合要求!";
return false;
}
System.IO.FileInfo info = new System.IO.FileInfo(path + file);
if (!System.IO.File.Exists(path))
{
System.IO.Directory.CreateDirectory(info.Directory.FullName);
}
System.IO.File.WriteAllBytes(path + file, buffer);
fileName = file;
returnMsg = "上传成功!";
return true;
}
|