c# Function Converting 2D Array to Image Problem

3399 views c#
1

I Made this Function:

  public Image intArrayToImage(int[] inputarray)
    {
        MemoryStream ms = new MemoryStream(inputarray);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }

And in the main function:

Color[,] finalarray= new Color[1600, 1600];
finalarray = intingArrayToImage(grayscale_map));

where grayscale_map is a 2d array "grayscale_map[1600x1600]"

But There is an error like this:

CS1503 C# Argument 1: cannot convert from 'int[ *, * ]' to 'int[ ]'

What should i do...

answered question

The problem is that intArrayToImage has as an argument an array of integers (one dimensional) and you pass a two dimensional array of integers.

1 Answer

8

You could re-declare your method, intArrayToImage as below:

public Image TwoDimensionalArrayToImage(int[,] twoDimensionalArray)
{
    using(var binaryFormatter = new BinaryFormatter())
    {
        using(var memoryStream = new MemoryStream())
        {
            binaryFormatter.Serialize(memoryStream, twoDimensionalArray);
            return Image.FromStream(memoryStream);
        }
    }
}

posted this

Have an answer?

JD

Please login first before posting an answer.