using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;namespace CSammisRun.Imaging{ ////// A base class which represents a 1bpp TIFF image /// public class OneBppImage { ////// Initializes a new 1bpp image from a TIFF file /// public OneBppImage(string fileName) { using (Bitmap bitmap = new Bitmap(fileName)) { FromBitmap(bitmap); } } ////// Initializes a new 1bpp image from a public OneBppImage(Bitmap bitmap) { FromBitmap(bitmap); } ////// /// Copies the specified public OneBppImage(OneBppImage image) { this.Height = image.Height; this.Width = image.Width; this.ImageData = new byte[Width, Height]; for (int x = 0; x < Width; x++) for (int y = 0; y < Height; y++) this.ImageData[x, y] = image.ImageData[x, y]; } ///into the new instance /// /// Initializes a new 1bpp image from an array in memory /// public OneBppImage(byte[,] imageData) { this.Width = imageData.GetLength(0); this.Height = imageData.GetLength(1); this.ImageData = imageData; } ////// Gets the image data as an array of bytes which are either public byte[,] ImageData { get; private set; } ////// or /// /// Gets the width of the image /// public int Width { get; private set; } ////// Gets the height of the image /// public int Height { get; private set; } #region Bitmap read/write methods ////// Reads pixel data from a 1bpp ////// Use a private void FromBitmap(Bitmap pageImage) { if (pageImage.PixelFormat != PixelFormat.Format1bppIndexed) { throw new ArgumentException("pageImage.PixelFormat"); } this.Height = pageImage.Height; this.Width = pageImage.Width; this.ImageData = new byte[this.Width, this.Height]; // Extract the pixel data from the bitmap BitmapData data = pageImage.LockBits(new Rectangle(0, 0, pageImage.Width, pageImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); int dataLength = data.Stride * data.Height; byte[] bmpData = new byte[dataLength]; Marshal.Copy(data.Scan0, bmpData, 0, dataLength); pageImage.UnlockBits(data); for (int y = 0; y < data.Height; y++) { int bmpDataRowBase = y * data.Stride; for (int x = 0; x < this.Width; x++) { int bmpDataIndex = bmpDataRowBase + (x >> 3); // The index where the pixel is stored sub-byte byte mask = (byte)(0x80 >> (x & 0x07)); int pixel = (bmpData[bmpDataIndex] & mask); this.ImageData[x, y] = (byte)((pixel == 0) ? Constants.PIXEL_VALUE_INK : Constants.PIXEL_VALUE_WHITESPACE); } } } ///to get a 1bpp bitmap from a more complex image. /// Create a 32bpp ARGB public virtual Bitmap CreateAsBitmap() { Bitmap retval = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb); BitmapData bmpData = retval.LockBits(new Rectangle(0, 0, this.Width, this.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int dataLength = bmpData.Stride * bmpData.Height; byte[] data = new byte[dataLength]; for (int y = 0, dataIndex = 0; y < this.Height; y++) { int rowBase = y * bmpData.Stride; for (int x = 0; x < this.Width; x++) { byte pixel = this.ImageData[x, y]; int finalIndex = rowBase + (x * 4); byte r = pixel, g = pixel, b = pixel; // Write the byte values in BGRA order, full opacity for A data[finalIndex] = b; data[finalIndex + 1] = g; data[finalIndex + 2] = r; data[finalIndex + 3] = 0xFF; dataIndex++; } } Marshal.Copy(data, 0, bmpData.Scan0, data.Length); retval.UnlockBits(bmpData); return retval; } #endregion Image read/write methods }}from the 1bpp image ///