///// <summary> ///// Converts an image to grayscale. ///// http://bobpowell.net/grayscale.aspx ///// http://code.msdn.microsoft.com/windowsdesktop/ColorMatrix-Image-Filters-f6ed20ae ///// </summary> ///// <param name="img"></param> ///// <returns></returns> //public static Bitmap ConvertGrayscale1(Bitmap img) //{ // Bitmap dest = new Bitmap(img.Width, img.Height); // dest.SetResolution(img.HorizontalResolution, img.VerticalResolution); // ColorMatrix cm = new ColorMatrix(new float[][]{ // new float[]{0.3f,0.3f,0.3f,0,0}, // new float[]{0.59f,0.59f,0.59f,0,0}, // new float[]{0.11f,0.11f,0.11f,0,0}, // new float[]{0,0,0,1,0,0}, // new float[]{0,0,0,0,1,0}, // new float[]{0,0,0,0,0,1}}); // ImageAttributes ia = new ImageAttributes(); // ia.SetColorMatrix(cm); // using (Graphics g = Graphics.FromImage(dest)) // { // g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); // } // return dest; //} /// <summary> /// Converts an image to 8bpp indexed grayscale. /// http://www.codeproject.com/Articles/70442/C-RGB-to-Palette-Based-bit-Greyscale-Bitmap-Clas /// </summary> /// <param name="input"></param> /// <returns></returns> public static Image ConvertGrayscale(Image input) { if (input.PixelFormat == PixelFormat.Format8bppIndexed) { return(input); // if already grayscale } byte[] gray = GrayBMP_File.CreateGrayBitmapArray(input); Image result = ImageConverter.byteArrayToImage(gray); ((Bitmap)result).SetResolution(input.HorizontalResolution, input.VerticalResolution); return(result); }
/// <summary> /// Convert an image to 8-bit indexed gray /// </summary> /// <param name="imgInput"></param> /// <returns></returns> public static Image ConvertGrays(Image imgInput) { if (imgInput.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { return(imgInput); //Check the input image and return itself if it being desired format } { byte[] gray = GrayBMP_File.CreateGrayBitmapArray(imgInput); Image result = ImageConverter.byteArrayToImage(gray); Bitmap tempResult = (Bitmap)result; tempResult.SetResolution(imgInput.HorizontalResolution, imgInput.VerticalResolution); return(tempResult); } }