如何1D字节数组转换为二维字节数组持有的位图? [英] How to convert 1D byte array to 2D byte array which holds a bitmap?

查看:150
本文介绍了如何1D字节数组转换为二维字节数组持有的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用LockBits和UnlockBits功能,并采取了图像的字节数组转换成一维数组。 (只考虑黑白/二值化图像)

I have already used the LockBits and UnlockBits functions and takes the byte array of a Image into a 1D array. (considering only the black and white/binarized images)

有它的考虑到一个二维数组(大小图像的高度和宽度)什么办法?所以我可以在阵列WIRTE成名为.txt文件,并查看它?

Is there any way of taking it to a 2D array (of the size image height and width)? so i can wirte the array into a ".txt" file and view it?

在code我已经习惯了把图像分为一维数组是如下:

The code i have used to take the image into 1D array is as below:

Public void function(Bitmap image){
{
byte[] arr1D;
byte[] arr2D;

BitmapData data = image.LockBits(new Rectangle(0, 0, img_w, img_h), ImageLockMode.ReadOnly, image.PixelFormat);
            try
            {
                IntPtr ptr = data.Scan0;
                int bytes = Math.Abs(data.Stride) * image.Height;
                byte[] rgbValues = new byte[bytes];
                arr1D = rgbValues;
                Marshal.Copy(ptr, rgbValues, 0, bytes);
            }
            finally
            {
                image.UnlockBits(data);
            }
}

由于图像是二进制的,将Byte数组的值是只有255,0。

Since the image is binary, the values of the Byte array is from only 255 and 0.

而不是整个图像中提取到一维数组,有没有任何方法/ code在那里我可以按行提取像素行到一个二维数组,在那里我可以把它写入一个文本文件,并在后面看到?

Instead of extracting the entire image to a 1D array, is there any method/code where i can extract pixel row by row to a 2D array, Where i can write it to a text file and see later on?

编程语言: C#

Programming Language : C#

例如:(如果值255替换为1)

example: (if the value 255 is replaced with 1)

结果输出:
一维数组:(6个像素点¯x6像素图片)

result output: 1D array: (6px X 6px image)

0 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 0 

期望的输出:
二维数组:(6个像素点¯x6像素图片)

expected output: 2D array: (6px X 6px image)

0 0 1 1 0 0
0 0 1 1 0 0 
1 1 1 1 1 1 
1 1 1 1 1 1
0 0 1 1 0 0 
0 0 1 1 0 0 

是否有人可以帮助我在C#中code呢?

推荐答案

下面是一个简单的函数,它接受一个一维int数组,你想将数据分成每行的大小,并返回一个二维数组。

Here's a simple function that takes a 1d int array and the size of each row you want the data split up into and returns a 2d array.

public int[,] ConvertArray(int[] Input, int size)
{
    int[,] Output = new int[(int)(Input.Length/size),size];
    System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\OutFile.txt");
    for (int i = 0; i < Input.Length; i += size)
    {
        for (int j = 0; j < size; j++)
        {
            Output[(int)(i / size), j] = Input[i + j];
            sw.Write(Input[i + j]);
        }
        sw.WriteLine("");
    }
    sw.Close();
    return Output;
}

我没加任何验证,以确保输入数组是由大小整除,如果需要的话你就需要添加。

I didn't add any validation to make sure the input array is exactly divisible by the size, if necessary you'll need to add that.

我确实添加了code将数据写入到文件中。

I did add code to write the data to a file.

这篇关于如何1D字节数组转换为二维字节数组持有的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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