c# base64轉(zhuǎn)字符串實(shí)例
轉(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)文章
C#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例
這篇文章主要介紹了C#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例,本文直接給出實(shí)現(xiàn)代碼例子,需要的朋友可以參考下2015-03-03C#判斷一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法
這篇文章主要介紹了C#判斷一個(gè)類是否實(shí)現(xiàn)了某個(gè)接口3種實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06c#不使用系統(tǒng)api實(shí)現(xiàn)可以指定區(qū)域屏幕截屏功能
這篇文章主要介紹了不使用系統(tǒng)API通過純c#實(shí)現(xiàn)屏幕指定區(qū)域截屏功能,截屏后還可以保存圖象文件,大家參考使用吧2014-01-01C# 靜態(tài)變量與靜態(tài)方法實(shí)例研究
寫了一個(gè)翻譯英漢單詞辭典的小程序,發(fā)現(xiàn)在調(diào)用幾千次的時(shí)候速度很慢2011-11-11C#實(shí)現(xiàn)23種常見的設(shè)計(jì)模式的示例詳解
設(shè)計(jì)模式通常分為三個(gè)主要類別:創(chuàng)建型模式、結(jié)構(gòu)型模式和行為型模式,這些模式是用于解決常見的對(duì)象導(dǎo)向設(shè)計(jì)問題的最佳實(shí)踐,本文為大家整理了23種常見的設(shè)計(jì)模式的實(shí)現(xiàn)代碼,需要的可以參考一下2023-06-06