简体   繁体   中英

Convert multidimensional array to grayscale image c#

I have created a program that generates a 256x256 matrix with random element values ranging from 0-255. I want to display this as a grayscale image. I am fairly certain I need to initialize a bitmap in order to display the image, however the image will not have color values, just the intensity values that are randomly initialized as elements of the matrix.

public static int[,] generateMatrix(int size = 256)
        {
            System.Random random = new System.Random();
            
            int[,] matrix = new int[256,256];
            
            for(int i=0; i<256; i++)
            {
                for(int j=0; j<256; j++)
                {
                    matrix[i,j] = random.Next(0,255);
                }
            }
            
            return matrix;
        }

How would I go about converting this matrix to a bitmap and furthermore to a grayscale image without requiring individual RGB values for each pixel? Thanks in advance to anybody who can help out.

I have created a program that generates a 256x256 matrix with random element values ranging from 0-255. I want to display this as a grayscale image. I am fairly certain I need to initialize a bitmap in order to display the image, however the image will not have color values, just the intensity values that are randomly initialized as elements of the matrix.

public static int[,] generateMatrix(int size = 256)
        {
            System.Random random = new System.Random();
            
            int[,] matrix = new int[256,256];
            
            for(int i=0; i<256; i++)
            {
                for(int j=0; j<256; j++)
                {
                    matrix[i,j] = random.Next(0,255);
                }
            }
            
            return matrix;
        }

How would I go about converting this matrix to a bitmap and furthermore to a grayscale image without requiring individual RGB values for each pixel? Thanks in advance to anybody who can help out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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