当前位置:首页 > 开发 > 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# 获取带有焦点的控件

this.ActiveControl...

C# 将函数作为参数传递

无参数    static void Main(s...

C# Control防闪烁

SetStyle(ControlStyles.AllPaintingInWmPaint |...

C# Graphics文本抗锯齿

g.TextRenderingHint = TextRenderingHint....

C# TextRenderer.MeasureText

TextRenderer.MeasureText是System.Windows.Forms命名空间中...

C# i++和++i的区别

核心区别操作顺序            ...