public static Bitmap GrayScale(Bitmap Bitmap) { Bitmap bitmap = (Bitmap)Bitmap.Clone(); for (int x = 0; x < bitmap.Width; ++x) { for (int y = 0; y < bitmap.Height; ++y) { Color grey = PrintingUtils.ColorToGrey(bitmap.GetPixel(x, y)); Bitmap.SetPixel(x, y, grey); } } return(Bitmap); }
public static Bitmap Monocrome(Bitmap imgSource, bool inverted) { Rectangle rect = new Rectangle(0, 0, imgSource.Width, imgSource.Height); Bitmap bitmap = new Bitmap(imgSource.Width, imgSource.Height, PixelFormat.Format1bppIndexed); BitmapData bitmapdata = imgSource.LockBits(rect, ImageLockMode.ReadOnly, imgSource.PixelFormat); BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); for (int y = 0; y < bitmap.Height; ++y) { for (int x = 0; x < bitmap.Width; ++x) { int ofs = y * bitmapdata.Stride + x * PrintingUtils.BitsPerPixel(imgSource) / 8; Color color = Color.FromArgb((int)Marshal.ReadByte(bitmapdata.Scan0, ofs + 2), (int)Marshal.ReadByte(bitmapdata.Scan0, ofs + 1), (int)Marshal.ReadByte(bitmapdata.Scan0, ofs)); if (!inverted && (double)color.GetBrightness() > 0.550000011920929 || inverted && inverted && (double)color.GetBrightness() < 0.550000011920929) { PrintingUtils.SetIndexedPixel(x, y, bitmapData, true); } } } bitmap.UnlockBits(bitmapData); imgSource.UnlockBits(bitmapdata); return(bitmap); }
public static string ImageToRaw(Bitmap bmBitmap) { BitmapData bitmapdata = (BitmapData)null; try { if (bmBitmap.PixelFormat != PixelFormat.Format1bppIndexed) { bmBitmap = PrintingUtils.Monocrome(bmBitmap, false); } bitmapdata = bmBitmap.LockBits(new Rectangle(0, 0, bmBitmap.Width, bmBitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); int length = bitmapdata.Height * bitmapdata.Stride; byte[] numArray = new byte[length]; Marshal.Copy(bitmapdata.Scan0, numArray, 0, length); return(HexEncoding.ToString(numArray)); } finally { if (bitmapdata != null) { bmBitmap.UnlockBits(bitmapdata); } } }