Accord.Imaging.Tools.ToBitmap C# (CSharp) Method

ToBitmap() private method

private ToBitmap ( this pixels ) : Bitmap
pixels this
return System.Drawing.Bitmap
        public static Bitmap ToBitmap(this byte[,] pixels)
        {
            int width = pixels.GetLength(1);
            int height = pixels.GetLength(0);

            Bitmap bitmap = Accord.Imaging.Image.CreateGrayscaleImage(width, height);

            BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height),
                ImageLockMode.WriteOnly, bitmap.PixelFormat);

            int offset = data.Stride - width;

            unsafe
            {
                byte* dst = (byte*)data.Scan0.ToPointer();

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, dst++)
                    {
                        *dst = (byte)pixels[y, x];
                    }
                    dst += offset;
                }
            }

            bitmap.UnlockBits(data);

            return bitmap;
        }

Same methods

Tools::ToBitmap ( this pixels, int width, int height ) : Bitmap
Tools::ToBitmap ( this pixels, int width, int height, double min, double max ) : Bitmap

Usage Example

Example #1
0
        public void ToBitmapTest1()
        {
            double[][] pixels =
            {
                new double[] { 0, 0, 0 }, new double[] { 0, 0, 1 }, new double[] { 0,   1, 0 },
                new double[] { 0, 1, 1 }, new double[] { 1, 0, 0 }, new double[] { 1,   0, 1 },
                new double[] { 1, 1, 0 }, new double[] { 1, 1, 1 }, new double[] { 0, 0.5, 0 },
            };

            int    width  = 3;
            int    height = 3;
            double min    = 0;
            double max    = 1;


            Bitmap actual = Tools.ToBitmap(pixels, width, height, min, max);

            // Accord.Controls.ImageBox.Show(actual, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(Color.FromArgb(000, 000, 000), actual.GetPixel(0, 0));
            Assert.AreEqual(Color.FromArgb(000, 000, 255), actual.GetPixel(1, 0));
            Assert.AreEqual(Color.FromArgb(000, 255, 000), actual.GetPixel(2, 0));

            Assert.AreEqual(Color.FromArgb(000, 255, 255), actual.GetPixel(0, 1));
            Assert.AreEqual(Color.FromArgb(255, 000, 000), actual.GetPixel(1, 1));
            Assert.AreEqual(Color.FromArgb(255, 000, 255), actual.GetPixel(2, 1));

            Assert.AreEqual(Color.FromArgb(255, 255, 000), actual.GetPixel(0, 2));
            Assert.AreEqual(Color.FromArgb(255, 255, 255), actual.GetPixel(1, 2));
            Assert.AreEqual(Color.FromArgb(000, 127, 000), actual.GetPixel(2, 2));

            double[][] actualArray = actual.ToDoubleArray(min, max);
            Assert.IsTrue(Matrix.IsEqual(pixels, actualArray, 0.01));
        }