IHaveSumQuestions
IHaveSumQuestions

Reputation: 163

C# convert a 2D double array to and greyscale image

first time working with C# here. I am reading a few images files, do some calculations, and output and array of double. I need to be able to save this double array (or these, since I will have multiples arrays) to a greyscale image. I have been looking around on the internet, I couldn't find much. i have done it on Python and Mathlab, but C# doesn't seems to be as friendly to me. here is what I have done so far (for the double image creation).

        static Image MakeImage(double[,] data)
    {
        Image img = new Bitmap(data.GetUpperBound(1), data.GetUpperBound(0));
        //Bitmap bitmap = new Bitmap(data.GetUpperBound(1), data.GetUpperBound(0));
        for (int i = 0; i < data.GetUpperBound(1); i++)
        {
            for (int k = 0; k < data.GetUpperBound(0); k++)
            {
                //bitmap.SetPixel(k, i, Color.FromArgb((int)data[i, k],(int) data[i, k],(int) data[i, k]));
            }
        }

        return img;
    }
}

}

This code actually doesnt do much. It create my blank image template. color doesnt take double as input. I have no Idea how to create an image from data... I am stuck =)

thank you in advance.

Upvotes: 2

Views: 2231

Answers (1)

Slippery Pete
Slippery Pete

Reputation: 3110

If you can accept using an unsafe block this is pretty fast:

    private Image CreateImage(double[,] data)
    {
        double min = data.Min();
        double max = data.Max();
        double range = max - min;
        byte v;

        Bitmap bm = new Bitmap(data.GetLength(0), data.GetLength(1));
        BitmapData bd = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

        // This is much faster than calling Bitmap.SetPixel() for each pixel.
        unsafe
        {
            byte* ptr = (byte*)bd.Scan0;
            for (int j = 0; j < bd.Height; j++)
            {
                for (int i = 0; i < bd.Width; i++)
                {
                    v = (byte)(255 * (data[i, bd.Height - 1 - j] - min) / range);
                    ptr[0] = v;
                    ptr[1] = v;
                    ptr[2] = v;
                    ptr[3] = (byte)255;
                    ptr += 4;
                }
                ptr += (bd.Stride - (bd.Width * 4));
            }
        }

        bm.UnlockBits(bd);
        return bm;

    }

Upvotes: 2

Related Questions