123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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;
- //}
- /// <summary>
- /// 将图片数据转换为Base64字符串
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- /// <summary>
- /// 将图片转转成base64编码的字符串
- /// </summary>
- /// <param name="Imagefilename"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 获取图片的宽度和高度
- /// </summary>
- /// <param name="phyPath"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 计算文件的 MD5 值
- /// </summary>
- /// <param name="fileName">要计算 MD5 值的文件名和路径</param>
- /// <returns>MD5 值16进制字符串</returns>
- public static string MD5File(string fileName)
- {
- return HashFile(fileName, "md5");
- }
- /// <summary>
- /// 计算文件的 sha1 值
- /// </summary>
- /// <param name="fileName">要计算 sha1 值的文件名和路径</param>
- /// <returns>sha1 值16进制字符串</returns>
- public static string SHA1File(string fileName)
- {
- return HashFile(fileName, "sha1");
- }
- /// <summary>
- /// 获取文件的SHA值
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 计算文件的哈希值
- /// </summary>
- /// <param name="fileName">要计算哈希值的文件名和路径</param>
- /// <param name="algName">算法:sha1,md5</param>
- /// <returns>哈希值16进制字符串</returns>
- 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);
- }
- /// <summary>
- /// 计算哈希值
- /// </summary>
- /// <param name="stream">要计算哈希值的 Stream</param>
- /// <param name="algName">算法:sha1,md5</param>
- /// <returns>哈希值字节数组</returns>
- 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);
- }
- /// <summary>
- /// 字节数组转换为16进制表示的字符串
- /// </summary>
- private static string ByteArrayToHexString(byte[] buf)
- {
- return BitConverter.ToString(buf).Replace("-", "");
- }
- }
- }
|