Convert int array To Bitmap - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Convert int array To Bitmap

Demo Code


using System.Linq;
using System.Drawing.Imaging;
using System.Drawing;
using System;/* w ww.j a v a 2s. c  o  m*/

public class Main{
        public static unsafe Bitmap ConvertToBitmap(this int[,] bytes)
        {
            var dimensions = new[]
            {
                bytes.GetLength(0),
                bytes.GetLength(1),
            };
            var width = dimensions[0];
            var height = dimensions[1];

            int maxValue = bytes.Cast<int>().Max();

            Bitmap bitmap  = new Bitmap(dimensions[0],dimensions[1]);

            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                {
                    Color color;
                    if (bytes[x, y] == maxValue)
                    {
                        color = Color.Red;
                    }
                    else
                    {
                        int value = (int)(255 - Math.Log10(1 + (double) bytes[x, y]*255/maxValue)*100);
                        
                        color = Color.FromArgb(value, value, value);
                    }
                    bitmap.SetPixel(x,y, color);
                }
            return bitmap;
        }
}

Related Tutorials