欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中圖片、二進(jìn)制與字符串的相互轉(zhuǎn)換方法

 更新時間:2016年06月09日 12:07:07   作者:smartsmile2012  
這篇文章主要介紹了C#中圖片、二進(jìn)制與字符串的相互轉(zhuǎn)換方法,涉及C#針對不同數(shù)據(jù)類型的解析與轉(zhuǎn)換操作技巧,需要的朋友可以參考下

本文實(shí)例講述了C#中圖片、二進(jìn)制與字符串的相互轉(zhuǎn)換方法。分享給大家供大家參考,具體如下:

protected void Button1_Click(object sender, EventArgs e)
{
  //圖片轉(zhuǎn)二進(jìn)制
  byte[] imageByte = GetPictureData(Server.MapPath("./uploadfile/111.png"));
  //二進(jìn)制轉(zhuǎn)換成字符串
  string picStr = Convert.ToBase64String(imageByte);
  //輸出字符串
  Response.Write(picStr);
  //字符串轉(zhuǎn)二進(jìn)制
  byte[] imageBytes = Convert.FromBase64String(picStr);
  //讀入MemoryStream對象
  MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
  memoryStream.Write(imageBytes, 0, imageBytes.Length);
  //二進(jìn)制轉(zhuǎn)成圖片保存
  System.Drawing.Image image = System.Drawing.Image.FromStream(memoryStream);
  image.Save(Server.MapPath("./uploadfile/222.png"));
}
/// <summary>
/// 二進(jìn)制流轉(zhuǎn)圖片
/// </summary>
/// <param name="streamByte">二進(jìn)制流</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;
}
/// <summary>
/// 圖片轉(zhuǎn)二進(jìn)制
/// </summary>
/// <param name="imagepath">圖片地址</param>
/// <returns>二進(jìn)制</returns>
public byte[] GetPictureData(string imagepath)
{
  //根據(jù)圖片文件的路徑使用文件流打開,并保存為byte[]
  FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重載方法
  byte[] byData = new byte[fs.Length];
  fs.Read(byData, 0, byData.Length);
  fs.Close();
  return byData;
}
/// <summary>
/// 圖片轉(zhuǎn)二進(jìn)制
/// </summary>
/// <param name="imgPhoto">圖片對象</param>
/// <returns>二進(jìn)制</returns>
public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
{
  //將Image轉(zhuǎn)換成流數(shù)據(jù),并保存為byte[]
  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;
}

PS:這里小編再給大家推薦本站的一款圖片轉(zhuǎn)BASE64格式的在線轉(zhuǎn)換工具,非常具有實(shí)用價值:

在線圖片轉(zhuǎn)換BASE64工具:
http://tools.jb51.net/transcoding/img2base64

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)

希望本文所述對大家C#程序設(shè)計有所幫助。

相關(guān)文章

最新評論