当前位置:首页 > 开发 > C# > 正文内容

C# Byte[]转为Image

C#1年前 (2024-12-30)

以下是在 C# 中将byte[](字节数组,通常表示图像的二进制数据)转换为Image类型的常见方法示例,这里假设字节数组中存储的是常见图像格式(如 JPEG、PNG 等)的数据,示例代码基于.NET Framework 或.NET Core/.NET 5 + 都适用:

方法一:使用MemoryStream结合Image.FromStream方法

public static Image ByteArrayToImage(byte[] byteArray)
{
    using (MemoryStream ms = new MemoryStream(byteArray))
    {
        return Image.FromStream(ms);
    }
}

你可以这样调用这个方法:

byte[] imageBytes = // 这里假设已经获取到了表示图像的字节数组,例如从文件读取等途径得到
Image image = ByteArrayToImage(imageBytes);
// 后续可以对Image对象进行相应的操作,比如显示在PictureBox控件中(在Windows Forms应用中)等


转载请注明出处。

本文链接:http://pythonopen.com/?id=1635

相关文章

C# 模拟按键

方法1SendKeys.SendWait("123{TAB}abc");&nbs...

C# CRC32算法

CRC32      class CRC32...

C# 获取网页源代码

private static string GetHtml(str...

C# string与StringBuilder速度测试

测试代码    Stopwatch time1 =...

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# double转为string并保留两位小数

在 C# 中,可以使用多种方式将 double 类型的数据转换为 string 类型并保留两位小数,...