在 C# 中,你可以使用 System.Drawing 命名空間中的 Bitmap 類來將 byte[] 轉(zhuǎn)換為 .jpg 圖片。以下是一個示例代碼:
using System;
using System.Drawing;
using System.IO;
class Program
{
static void Main()
{
byte[] imageBytes = GetImageBytes(); // 獲取圖片的 byte[] 數(shù)據(jù)
using (MemoryStream memoryStream = new MemoryStream(imageBytes))
{
using (Bitmap bitmap = new Bitmap(memoryStream))
{
string filePath = "image.jpg"; // 圖片保存路徑
//在這里修改不同的圖片格式,就可以保存為指定格式圖片System.Drawing.Imaging.ImageFormat.Jpeg
bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("圖片保存成功!");
}
}
}
static byte[] GetImageBytes()
{
// 在這里編寫獲取圖片 byte[] 數(shù)據(jù)的代碼
// 例如,你可以從文件、網(wǎng)絡(luò)等地方讀取圖片數(shù)據(jù),并返回 byte[] 數(shù)組
// 這里只是一個示例,你需要根據(jù)實(shí)際情況自行實(shí)現(xiàn)
byte[] imageBytes = new byte[0];
return imageBytes;
}
}
在上面的示例中,GetImageBytes 方法是一個用于獲取圖片的 byte[] 數(shù)據(jù)的示例方法。你需要根據(jù)實(shí)際情況自行實(shí)現(xiàn)該方法,從文件、網(wǎng)絡(luò)等地方讀取圖片數(shù)據(jù),并返回 byte[] 數(shù)組。
然后,我們使用 MemoryStream 將 byte[] 數(shù)據(jù)加載到 Bitmap 對象中,并使用 Save 方法將 Bitmap 對象保存為 .jpg 圖片文件。文章來源:http://www.zghlxwxcb.cn/news/detail-803397.html
請注意,在運(yùn)行此代碼之前,確保已添加對 System.Drawing 程序集的引用。你可以在 Visual Studio 中選擇項(xiàng)目->添加引用,然后在“程序集”選項(xiàng)卡中找到 System.Drawing 并添加它。文章來源地址http://www.zghlxwxcb.cn/news/detail-803397.html
到了這里,關(guān)于C# 使用Bitmap 將byte[] 轉(zhuǎn)成.jpg/.png/gif等圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!