繁体   English   中英

如何将3x3像素.bmp转换为3x3字节数组?

[英]How to convert a 3x3 pixel .bmp into a 3x3 byte array?

我遇到了一个难题,我需要将位图转换为字节数组,但是我需要采用某种方式,为了演示这些位图是单色的,这是我需要做的:

假设#是RGB值255、255、255的键,@是RGB值0、0、0。

@@@

@ ##

@#@

我需要将其转换成这样的东西:

0、0、0

0、255、255

0、255、0

能做到这一点吗?

首先获取字节:

ImageConverter

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

或内存流

public static byte[] ImageToByte2(Image img)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

然后将其转换为所需的多维数组。

byte[][] multi = new byte[height][];
for (int y = 0; y < height; ++y)
{
    multi[y] = new byte[width];
    // Do optional translation of the byte into your own format here
    // For purpose of illustration, here is a straight copy
    Array.Copy(bitmapBytes, width * y, multi[y], 0, width);
}

暂无
暂无

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

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