一、Base64編碼概述
Base64是一種編碼方式,將二進制數據轉換成 ASCII 字符集中的可打印字符。Base64 是網絡上最常見的用於傳輸二進制數據的編碼方式之一,它可以將圖片、音頻、視頻等二進制數據轉換成文本格式,方便傳輸和處理。
示例代碼:
public static string ToBase64String(byte[] bytes) { return Convert.ToBase64String(bytes); }
Base64編碼方式有多種,常用的有RFC3548規定的標準 Base64、URL-Safe Base64、直接將Base64編碼後的字符串去掉一些字符即可得到 Base64 Url。在C#中,可以使用 Convert.ToBase64String 方法將二進制數據轉換成 Base64 字符串。
二、圖片轉 Base64 的原理
在C#中,將圖片轉換成 Base64 字符串的原理非常簡單,就是將圖片二進制數據讀取出來,然後使用 Convert.ToBase64String 方法將二進制數據轉換成字符串:
示例代碼:
public static string ImageToBase64(Image image, ImageFormat format) { using(MemoryStream ms = new MemoryStream()) { //將圖像以指定的格式保存到流中 image.Save(ms, format); //將流轉換成字節數組 byte[] bytes = ms.ToArray(); //將字節數組轉換成 Base64 字符串 return Convert.ToBase64String(bytes); } }
上面的代碼中,使用了 C# 中的 Image 類和 ImageFormat 類讀取圖片數據,然後使用了 MemoryStream 類將數據保存到一個內存流中,最後使用 Convert.ToBase64String 方法將內存流中的數據轉換成 Base64 字符串。
三、C#圖片轉 Base64的應用
1、HTML/CSS中圖片轉 Base64
在HTML/CSS中,使用 Base64 可以將圖片直接嵌入到HTML/CSS代碼中,減少頁面的請求次數,加快頁面加載速度。下面是實現圖片轉 Base64 的代碼:
示例代碼:
<style type="text/css"> .demo { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAABkCAIAAACPX4IEWAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4xu2deZBU1bXvH+nl79l2yy7sddd999Hy7g6JkcAAAAASUVORK5CYII=); width: 100px; height: 100px; } </style> <div class="demo"></div>
上面的代碼中,通過將 Base64 編碼的圖片數據直接嵌入到 CSS 中,實現了在頁面中顯示圖片的效果。
2、ASP.NET 中圖片轉 Base64
在ASP.NET中,可以將圖片轉換成Base64字符串,然後將Base64字符串存儲在數據庫中,或者JSON數據中,方便傳輸。
示例代碼:
protected void Page_Load(object sender, EventArgs e) { string imageUrl = "http://www.example.com/image.jpg"; byte[] imageBytes = new WebClient().DownloadData(imageUrl); string base64String = Convert.ToBase64String(imageBytes); Response.Write(base64String); }
上面的代碼中,使用了 WebClient 類從遠程網站獲取圖片二進制數據,然後使用 Convert.ToBase64String 方法將二進制數據轉換成 Base64 字符串,最後將 Base64 字符串寫入 HttpResponse 中返回前端頁面。
3、Windows Form 中圖片轉 Base64
在 Windows Form 中,可以將圖片轉換成 Base64 字符串,然後進行加密、顯示等操作。
示例代碼:
private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "圖片文件|*.jpg;*.jpeg;*.bmp;*.png;*.gif"; if (dialog.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(dialog.FileName, FileMode.Open)) { //從文件流中讀取圖片數據 byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); //將圖片數據轉換成 Base64 字符串 string base64String = Convert.ToBase64String(bytes); //將 Base64 字符串顯示在文本框中 txtResult.Text = base64String; } } }
上面的代碼中,使用了 OpenFileDialog 類從本地文件打開圖片文件,然後使用 FileStream 類從文件流中讀取圖片數據,接着使用 Convert.ToBase64String 方法將圖片數據轉換成 Base64 字符串,最後將 Base64 字符串顯示在 Windows Form 界面的文本框中。
4、Web API 中圖片轉 Base64
在 Web API 中,可以將圖片轉換成 Base64 字符串,然後將 Base64 字符串作為 JSON 數據返回給前端頁面。
示例代碼:
[HttpGet] [Route("getimage")] public IHttpActionResult GetImage(string imageUrl) { byte[] imageBytes = new WebClient().DownloadData(imageUrl); string base64String = Convert.ToBase64String(imageBytes); return Json(new { imageBase64 = base64String }); }
上面的代碼中,使用了 Web API 的 Get 方法獲取遠程圖片,然後使用 Convert.ToBase64String 方法將圖片數據轉換成 Base64 字符串,最後將 Base64 字符串作為 JSON 數據返回給前端頁面。
5、WPF 中圖片轉 Base64
在 WPF 中,可以將圖片轉換成 Base64 字符串,然後將 Base64 字符串顯示在 WPF 界面的圖片控件中。
示例代碼:
private void btnOpen_Click(object sender, RoutedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "圖片文件|*.jpg;*.jpeg;*.bmp;*.png;*.gif"; if (dialog.ShowDialog() == true) { using (MemoryStream ms = new MemoryStream()) { //從文件流中讀取圖片數據 BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.UriSource = new Uri(dialog.FileName); bi.EndInit(); //將圖片數據轉換成 Base64 字符串 JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bi)); encoder.Save(ms); byte[] bytes = ms.ToArray(); string base64String = Convert.ToBase64String(bytes); //將 Base64 字符串顯示在圖片控件中 imgPhoto.Source = bi; } } }
上面的代碼中,使用了 OpenFileDialog 類從本地文件打開圖片文件,然後使用 BitmapImage 類從文件流中讀取圖片數據,接着使用 JpegBitmapEncoder 類將圖片數據轉換成 Base64 字符串,最後將 Base64 字符串顯示在 WPF 界面的圖片控件中。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/245766.html