简体   繁体   English

使用ImageSharp从byte []创建图像

[英]Create an image from byte[] using ImageSharp

I have a byte[] and I want to create a gray-scale image using ImageSharp library. 我有一个byte[] ,我想使用ImageSharp库创建一个灰度图像。

That is how I am doing it currently: 这就是我目前正在做的事情:

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];
    }
}

I wonder if there is a more elegant way to do it ? 我想知道是否有更优雅的方法?

You could do it in this way: 您可以通过以下方式进行操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM