using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using WebUI.ServiceReferenceUser; using System.Text; namespace WebUI.Controllers { public class DefaultController : Controller { // GET: Default public ActionResult Index() { return View(); } #region 文件转二进制流(字节数组) /// <summary> /// 文件转二进制流(字节数组) /// </summary> /// <param name="strpath"></param> /// <returns></returns> public byte[] returnbyte(string strpath) { // 以二进制方式读文件 FileStream fsMyfile = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite); // 创建一个二进制数据流读入器,和打开的文件关联 BinaryReader brMyfile = new BinaryReader(fsMyfile); // 把文件指针重新定位到文件的开始 brMyfile.BaseStream.Seek(0, SeekOrigin.Begin); byte[] bytes = brMyfile.ReadBytes(Convert.ToInt32(fsMyfile.Length.ToString())); // 关闭以上new的各个对象 brMyfile.Close(); return bytes; } #endregion #region 图片转二进制流(字节数组) /// <summary> /// 图片转二进制流(字节数组) /// </summary> /// <param name="Imagefilename"></param> /// <returns></returns> private byte[] ImgToByte(string Imagefilename) { 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 arr; } #endregion #region 二进制流转图片 /// <summary> /// 二进制流转图片 /// </summary> /// <param name="arr"></param> /// <param name="FileName"></param> private void ByteToImg(byte[] arr, string FileName) { MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms); bmp.Save(FileName); } #endregion #region 图片转Base64字符串 /// <summary> /// 图片转Base64字符串 /// </summary> /// <param name="Imagefilename"></param> /// <returns></returns> private string ImgToBase64String(string Imagefilename) { 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(); String strbaser64 = Convert.ToBase64String(arr); return strbaser64; } #endregion #region Base64字符串转图片 /// <summary> /// Base64字符串转图片 /// </summary> /// <param name="Base64Str"></param> /// <param name="FileName"></param> private void Base64StringToImg(string Base64Str, string FileName) { MemoryStream ms = new MemoryStream(Convert.FromBase64String(Base64Str)); Bitmap bmp = new Bitmap(ms); bmp.Save(FileName); } #endregion #region Base64和字节数组互转 /// <summary> /// Base64转字节数组 /// </summary> /// <param name="Base64Str"></param> /// <returns></returns> private byte[] Base64StringToByte(string Base64Str) { return Convert.FromBase64String(Base64Str); } /// <summary> /// 字节数组转Base64 /// </summary> /// <param name="arr"></param> /// <returns></returns> private string ByteToBase64String(byte[] arr) { return Convert.ToBase64String(arr); } #endregion #region 字节流与二进制互转 /// <summary> /// 字节流与二进制互转 /// </summary> public void ByteToBit() { var bytearr = ImgToByte(Server.MapPath("/images/teacher.jpg")); StringBuilder str = new StringBuilder(); foreach (var p in bytearr) { str.AppendFormat("{0},", Convert.ToString(p, 2)); } var imgstr = str.ToString().Trim(','); FileHelper.WriteText(Server.MapPath("/bit.txt"), imgstr); var arr = FileHelper.FileToString(Server.MapPath("/bit.txt")).Split(','); var imgarr = Array.ConvertAll<string, byte>(arr, m => Convert.ToByte(m, 2)); } #endregion /// <summary> /// 测试输出图片 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public ActionResult Pic(string fileName) { return File(Base64StringToByte(FileHelper.FileToString(fileName)), @"image/jpeg"); } } }