using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Ant.Service.Utility
{
public class FileCommon
{
public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
//public String ConvertPictureToString(string imagepath)
//{
// /**/
// ////根据图片文件的路径使用文件流打开,并保存为String
// byte[] byData = ConvertPictureToByte(imagepath);
// String SData = Convert.ToBase64String(byData);
// return SData;
//}
///
/// 将图片数据转换为Base64字符串
///
///
///
public static string ImgToBase64(Image img)
{
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream();
binFormatter.Serialize(memStream, img);
byte[] bytes = memStream.GetBuffer();
string base64 = Convert.ToBase64String(bytes);
return base64;
}
///
/// 将图片转转成base64编码的字符串
///
///
///
public static string ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return null;
}
}
//base64编码的字符串转为图片
public static Bitmap Base64StringToImage(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
//bmp.Save("test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save("test.bmp", ImageFormat.Bmp);
//bmp.Save("test.gif", ImageFormat.Gif);
//bmp.Save("test.png", ImageFormat.Png);
ms.Close();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
///
/// 获取图片的宽度和高度
///
///
///
public static string GetThumbnail(string phyPath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
int orignWidth = image.Width; //原图尺寸
int orignHeight = image.Height;
return orignWidth + ";" + orignHeight;
}
///
/// 计算文件的 MD5 值
///
/// 要计算 MD5 值的文件名和路径
/// MD5 值16进制字符串
public static string MD5File(string fileName)
{
return HashFile(fileName, "md5");
}
///
/// 计算文件的 sha1 值
///
/// 要计算 sha1 值的文件名和路径
/// sha1 值16进制字符串
public static string SHA1File(string fileName)
{
return HashFile(fileName, "sha1");
}
///
/// 获取文件的SHA值
///
///
///
public static string GetFileSHA(string fileName)
{
string filehash = string.Empty;
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
byte[] buffer;
using (HashAlgorithm hash = HashAlgorithm.Create())
{
buffer = hash.ComputeHash(fs);
hash.Clear();
}
filehash = Convert.ToBase64String(buffer);
}
}
catch (Exception ex)
{
}
return filehash;
}
///
/// 计算文件的哈希值
///
/// 要计算哈希值的文件名和路径
/// 算法:sha1,md5
/// 哈希值16进制字符串
private static string HashFile(string fileName, string algName)
{
if (!System.IO.File.Exists(fileName))
return string.Empty;
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] hashBytes = HashData(fs, algName);
fs.Close();
return ByteArrayToHexString(hashBytes);
}
///
/// 计算哈希值
///
/// 要计算哈希值的 Stream
/// 算法:sha1,md5
/// 哈希值字节数组
private static byte[] HashData(System.IO.Stream stream, string algName)
{
System.Security.Cryptography.HashAlgorithm algorithm;
if (algName == null)
{
throw new ArgumentNullException("algName 不能为 null");
}
if (string.Compare(algName, "sha1", true) == 0)
{
algorithm = System.Security.Cryptography.SHA1.Create();
}
else
{
if (string.Compare(algName, "md5", true) != 0)
{
throw new Exception("algName 只能使用 sha1 或 md5");
}
algorithm = System.Security.Cryptography.MD5.Create();
}
return algorithm.ComputeHash(stream);
}
///
/// 字节数组转换为16进制表示的字符串
///
private static string ByteArrayToHexString(byte[] buf)
{
return BitConverter.ToString(buf).Replace("-", "");
}
}
}