在 Core 2.1 中将 byte[] 转换为 Bitmap [英] Convert byte[] to Bitmap in Core 2.1

查看:25
本文介绍了在 Core 2.1 中将 byte[] 转换为 Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 .Net 4.6.2 中运行良好的项目,它大量使用了将 byte[] 转换为 Bitmap.

I had a project working well in .Net 4.6.2, which makes heavy usage of converting byte[] to Bitmap.

public static Bitmap ByteArrayToImage(byte[] source)
    {
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
        return (Bitmap)tc.ConvertFrom(source);
    }

但是,我已经将项目升级到 .Net Core 2.1,但现在不再有效.我读过,有些人有问题,但正在努力寻找解决方案.

However, I have since upgraded projects to .Net Core 2.1 and this no longer works. I've read, and a few people have issues, but battling to find a fix.

TypeConverter 不能从 System.Byte[] 转换

TypeConverter cannot convert from System.Byte[]

2.1 有没有办法实现这种转换?看起来 https://github.com/SixLabors/ImageSharp 可能有用,但是当我搜索在 Nuget 中,我没有得到任何结果.

Is there a way in 2.1 to achieve this conversion? It looks like https://github.com/SixLabors/ImageSharp might work, but when I search for it in Nuget, I get no results.

推荐答案

您需要将 bytes 放入 MemoryStream

public static Bitmap ByteArrayToImage(byte[] source)
{
    using (var ms = new MemoryStream(source))
    {
        return new Bitmap(ms);
    }
}

上面的代码将使用Bitmap(Stream stream)构造函数.

The code above will use the Bitmap(Stream stream) constructor.

这篇关于在 Core 2.1 中将 byte[] 转换为 Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆