2

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.

  • possible duplicate of [Convert an image to grayscale](http://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale) – Mohsen Esmailpour Sep 26 '14 at 20:37
  • Your code is ok, what's the content of data? – brz Sep 26 '14 at 20:58
  • Hi, the code currently only creates a new bitmap image. My problem is, I want to be able to take a certain DOUBLE[,] data and make it an image. for instance : [[1.1,2.5],[4.2,3.3]] -> have a picture (bitmap image) with these data inside. (right now, it create a blank image, and the commented part creates a INTEGER image). – IHaveSumQuestions Sep 27 '14 at 20:50

1 Answers1

2

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;

    }
Slippery Pete
  • 2,949
  • 1
  • 10
  • 13