图片与Byte相互转换,文件和字节流的转换方法
/// <summary>
????????/// 文件轉(zhuǎn)化成byte[]數(shù)組
????????/// </summary>
????????/// <param name="fileName"></param>
????????/// <returns></returns>
????????private byte[] FileContent(string fileName)
????????{
????????????FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
????????????try
????????????{
????????????????byte[] buffur = new byte[fs.Length];
????????????????fs.Read(buffur, 0, (int)fs.Length);
????????????????return buffur;
????????????}
????????????catch (Exception ex)
????????????{
????????????????//ex.Message
????????????????return null;
????????????}
????????????finally
????????????{
????????????????if (fs != null)
????????????????{
????????????????????fs.Close();
????????????????}
????????????}
????????}
????????/// <summary>
????????/// C#文件和字節(jié)流的轉(zhuǎn)換方法
????????/// </summary>
????????/// <param name="buffur"></param>
????????/// <param name="fs"></param>
????????/// <param name="filename"></param>
????????public void FileFun(byte[] buffur,FileStream fs,string filename)
????????{
????????????//將byte 保存為文件 轉(zhuǎn)為文件
????????????using (fs = new FileStream("D://test.jpg", FileMode.Create, FileAccess.Write))
????????????{
????????????????fs.Write(buffur, 0, buffur.Length);
????????????};
????????????fs.Close();
????????????//C#文件和字節(jié)流的轉(zhuǎn)換方法
????????????//1、讀取文件,并轉(zhuǎn)換為字節(jié)流
????????????fs = new FileStream(filename,FileMode.Open,FileAccess.Read);
????????????byte[] infbytes = new byte[(int)fs.Length];
????????????fs.Read(infbytes, 0, infbytes.Length);
????????????fs.Close();
????????????//2、將字節(jié)流寫(xiě)入文件
????????????fs = new FileStream("D:\inf.dlv",FileMode.Create,FileAccess.Write);
????????????fs.Write(infbytes, 0, infbytes.Length);
????????????fs.Close();
????????}
????????//C# 圖片與Byte相互轉(zhuǎn)換
????????/// <summary>
????????/// 圖片轉(zhuǎn)byte
????????/// </summary>
????????/// <param name="PicturePath"></param>
????????/// <returns></returns>
????????private byte[] GetIMGbyte(string PicturePath)
????????{
????????????//將需要存儲(chǔ)的圖片讀取為數(shù)據(jù)流
????????????FileStream fs = new FileStream(@PicturePath, FileMode.Open, FileAccess.Read);
????????????Byte[] btye2 = new byte[fs.Length];
????????????fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
????????????fs.Close();
????????????return btye2;
????????}
????????/// <summary>
????????/// byte轉(zhuǎn)圖片
????????/// </summary>
????????/// <param name="streamByte"></param>
????????/// <returns></returns>
????????public System.Drawing.Image ReturnPhoto(byte[] streamByte)
????????{
????????????//將數(shù)據(jù)流讀取為圖片
????????????System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
????????????System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
????????????return img;
????????}
????????/// <summary>
????????/// 圖片轉(zhuǎn)byte
????????/// </summary>
????????/// <param name="imagePath"></param>
????????/// <returns></returns>
????????public byte[] GetPictureData(string imagePath)
????????{
????????????FileStream fs = new FileStream(imagePath, FileMode.Open);
????????????byte[] byteData = new byte[fs.Length];
????????????fs.Read(byteData, 0, byteData.Length);
????????????fs.Close();
????????????return byteData;
????????}
?????????//將Image轉(zhuǎn)換成流數(shù)據(jù),并保存為byte[]?
????????/// <summary>
????????/// 圖片轉(zhuǎn)byte
????????/// </summary>
????????/// <param name="imgPhoto"></param>
????????/// <returns></returns>
????????public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
????????{
????????????MemoryStream mstream = new MemoryStream();
????????????imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
????????????byte[] byData = new Byte[mstream.Length];
????????????mstream.Position = 0;
????????????mstream.Read(byData, 0, byData.Length); mstream.Close();
????????????return byData;
????????}
????????//圖片的“寫(xiě)”操作
????????//參數(shù)是Byte[]類(lèi)型,返回值是Image對(duì)象
????????/// <summary>
????????/// byte轉(zhuǎn)圖片
????????/// </summary>
????????/// <param name="streamByte"></param>
????????/// <returns></returns>
????????public System.Drawing.Image ReturnPhoto(byte[] streamByte)
????????{
????????????System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
????????????System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
????????????return img;
????????}
????????//參數(shù)是Byte[] 類(lèi)型,沒(méi)有返回值(ASP.NET輸出圖片)
????????public void WritePhoto(byte[] streamByte)
????????{
????????????// Response.ContentType 的默認(rèn)值為默認(rèn)值為“text/html”
????????????Response.ContentType = "image/GIF";
????????????//圖片輸出的類(lèi)型有: image/GIF ????image/JPEG
????????????Response.BinaryWrite(streamByte);
????????}
?
總結(jié)
以上是生活随笔為你收集整理的图片与Byte相互转换,文件和字节流的转换方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: script 标签到底该放在哪里
- 下一篇: html 标签内背景图片自适应 div