【问题标题】:Create an image from byte[] using ImageSharp使用 ImageSharp 从 byte[] 创建图像
【发布时间】:2018-01-09 21:57:53
【问题描述】:

我有一个byte[],我想使用ImageSharp 库创建一个灰度图像。

这就是我目前的做法:

byte[] arr = MnistReader.ReadTestData(); //this gives me byte[] with 784 pixel values

ImageSharp.Image image = new ImageSharp.Image(28, 28);

for (int i = 0; i < image.Height; i++)
{
    for (int j = 0; j < image.Width; j++)
    {
        int curPixel = j + i * image.Width;
        image.Pixels[curPixel].R = arr[curPixel];
        image.Pixels[curPixel].G = arr[curPixel];
        image.Pixels[curPixel].B = arr[curPixel];
    }
}

不知道有没有更优雅的方法呢?

【问题讨论】:

    标签: c# .net-core imagesharp


    【解决方案1】:

    你可以这样做:

    var image = Image.Load<Rgba32>(byteArray);
    image.Mutate(x => x.Grayscale());
    

    【讨论】:

    • 是的,这是最好的方法。还有重载可以选择要使用的算法。
    猜你喜欢
    • 2017-07-06
    • 2014-04-17
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2022-06-12
    • 1970-01-01
    相关资源
    最近更新 更多