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

c# base64轉(zhuǎn)字符串實(shí)例

 更新時(shí)間:2020年12月08日 11:40:03   作者:萌橙  
這篇文章主要介紹了c# base64轉(zhuǎn)字符串實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

轉(zhuǎn)成 Base64 形式的 System.String:

string a = "base64字符串與普通字符串互轉(zhuǎn)"; 
 byte[] b = System.Text.Encoding.Default.GetBytes(a); 
 //轉(zhuǎn)成 Base64 形式的 System.String 
 a = Convert.ToBase64String(b); 
 Response.Write(a); 

轉(zhuǎn)回到原來的 System.String:

 byte[] c = Convert.FromBase64String(a); 
 a = System.Text.Encoding.Default.GetString(c); 
 Response.Write(a);

補(bǔ)充知識(shí):c# 中base64字符串和圖片的相互轉(zhuǎn)換

c#base64字符串轉(zhuǎn)圖片用到了bitmap類,封裝 GDI+ 位圖,此位圖由圖形圖像及其特性的像素?cái)?shù)據(jù)組成。 Bitmap 是用于處理由像素?cái)?shù)據(jù)定義的圖像的對(duì)象。

具體bitmap類是什么可以自己百度查詢,這里就不多介紹了。

#region base64轉(zhuǎn)圖片
    /// <summary>
    /// 圖片上傳 Base64解碼
    /// </summary>
    /// <param name="dataURL">Base64數(shù)據(jù)</param>
    /// <param name="imgName">圖片名字</param>
    /// <returns>返回一個(gè)相對(duì)路徑</returns>
    public string decodeBase64ToImage(string dataURL,string imgName)
    {
 
      string filename = "";//聲明一個(gè)string類型的相對(duì)路徑
 
      String base64 = dataURL.Substring(dataURL.IndexOf(",") + 1);   //將‘,'以前的多余字符串刪除
      System.Drawing.Bitmap bitmap = null;//定義一個(gè)Bitmap對(duì)象,接收轉(zhuǎn)換完成的圖片
 
      try//會(huì)有異常拋出,try,catch一下
      {
 
        byte[] arr = Convert.FromBase64String(base64);//將純凈資源Base64轉(zhuǎn)換成等效的8位無符號(hào)整形數(shù)組
 
        System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);//轉(zhuǎn)換成無法調(diào)整大小的MemoryStream對(duì)象
        bitmap = new System.Drawing.Bitmap(ms);//將MemoryStream對(duì)象轉(zhuǎn)換成Bitmap對(duì)象
 
        filename ="Knowledge_" + imgName + ".jpg";//所要保存的相對(duì)路徑及名字
        string url =HttpRuntime.AppDomainAppPath.ToString();
        string tmpRootDir = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString()); //獲取程序根目錄 
        string imagesurl2 = tmpRootDir + filename; //轉(zhuǎn)換成絕對(duì)路徑 
        bitmap.Save(imagesurl2, System.Drawing.Imaging.ImageFormat.Jpeg);//保存到服務(wù)器路徑
        //bitmap.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
        //bitmap.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
        //bitmap.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
        ms.Close();//關(guān)閉當(dāng)前流,并釋放所有與之關(guān)聯(lián)的資源
        bitmap.Dispose();
      }
      catch (Exception e)
      {
        string massage= e.Message;
      }
      return filename;//返回相對(duì)路徑
    }
    #endregion
 
    #region 圖片轉(zhuǎn)base64
    /// <summary>
    /// 圖片轉(zhuǎn)base64
    /// </summary>
    /// <param name="path">圖片路徑</param><br>    /// <returns>返回一個(gè)base64字符串</returns>
    public string decodeImageToBase64(string path) {
      path = "E:/vs 2015/newaqtcprj/WEB/UpFile/2018/12/20181229174708_7471722c425a2ec08fa513ddf4f8c76306d55fbb0fbd9d8.jpg";
      string base64str = "";
 
      //站點(diǎn)文件目錄
      string fileDir = HttpContext.Current.Server.MapPath("/");
      string[] arrfileDir = fileDir.Split('\\');
      fileDir = arrfileDir[0] + "\\" + arrfileDir[1] + "\\" + arrfileDir[2];
      try
      {
        //讀圖片轉(zhuǎn)為Base64String
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Path.Combine(fileDir, "WEB\\UpFile\\2018\\12\\20181229174708_7471722c425a2ec08fa513ddf4f8c76306d55fbb0fbd9d8.jpg"));
        //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(path);
        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();
        bmp.Dispose();
        base64str = Convert.ToBase64String(arr);
      }
      catch (Exception e)
      {
        string mss = e.Message;
      }
      return "data:image/jpg;base64," + base64str;
    }
    #endregion

以上這篇c# base64轉(zhuǎn)字符串實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論