using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class PictureBoxForm : Form
public PictureBoxForm(Bitmap image)
this.pictureBox1.Image = image as System.Drawing.Image;
public PictureBoxForm(Bitmap image1, Bitmap image2)
this.pictureBox1.Image = image1 as System.Drawing.Image;
this.pictureBox2.Image = image2 as System.Drawing.Image;
partial class PictureBoxForm
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
if (disposing && (components != null))
#region Windows Form Designer generated code
private void InitializeComponent()
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(600, 600);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox2.Location = new System.Drawing.Point(618, 12);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(250, 250);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(882, 625);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "PictureBoxForm";
this.Text = "PictureBoxForm";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
public partial class Grayscale
public static unsafe Bitmap ToGrayscale(Bitmap bmColor)
int Width = bmColor.Width;
int Height = bmColor.Height;
Bitmap bmMono = new Bitmap(Width, Height, PixelFormat.Format8bppIndexed);
bmMono.SetResolution(bmColor.HorizontalResolution, bmColor.VerticalResolution);
ColorPalette colorPalette = bmMono.Palette;
for (int i = 0; i < colorPalette.Entries.Length; i++)
colorPalette.Entries[i] = Color.FromArgb(i, i, i);
bmMono.Palette = colorPalette;
BitmapData bmd = bmMono.LockBits(new Rectangle(Point.Empty, bmMono.Size), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
Byte* pPixel = (Byte*)bmd.Scan0;
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
Color clr = bmColor.GetPixel(x, y);
Byte byPixel = (byte)((30 * clr.R + 59 * clr.G + 11 * clr.B) / 100);
public static void SetGrayscalePalette(Bitmap image)
if (image.PixelFormat == PixelFormat.Format8bppIndexed)
ColorPalette cp = image.Palette;
for (int i = 0; i < 256; i++)
cp.Entries[i] = Color.FromArgb(i, i, i);
throw new Exception("8 bit grayscaleImage required");
public static Bitmap CreateGrayscaleImage(int width, int height)
Bitmap image = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
SetGrayscalePalette(image);
public static bool IsGrayscale(Bitmap bitmap)
return bitmap.PixelFormat == PixelFormat.Format8bppIndexed ? true : false;
public class BitmapLocker
BitmapData _bitmapData = null;
private byte[] _imageData = null;
public IntPtr IntegerPointer
return _bitmapData.Stride;
return Bitmap.GetPixelFormatSize(_bitmap.PixelFormat);
return _bitmapData.Stride - (_bitmap.Width * ColorDepth / 8);
public PixelFormat ImagePixelFormat
return _bitmap.PixelFormat;
return Grayscale.IsGrayscale(_bitmap);
public BitmapLocker(Bitmap source)
IntegerPointer = IntPtr.Zero;
public Color GetPixel(int x, int y)
int cCount = ColorDepth / 8;
int i = ((y * Width) + x) * cCount;
if (i > _imageData.Length - cCount)
throw new IndexOutOfRangeException();
byte g = _imageData[i + 1];
byte r = _imageData[i + 2];
byte a = _imageData[i + 3];
clr = Color.FromArgb(a, r, g, b);
byte g = _imageData[i + 1];
byte r = _imageData[i + 2];
clr = Color.FromArgb(r, g, b);
clr = Color.FromArgb(c, c, c);
public void SetPixel(int x, int y, Color color)
int cCount = ColorDepth / 8;
int i = ((y * Width) + x) * cCount;
_imageData[i + 1] = color.G;
_imageData[i + 2] = color.R;
_imageData[i + 3] = color.A;
_imageData[i + 1] = color.G;
_imageData[i + 2] = color.R;
string str = string.Empty;
_bitmapData = _bitmap.LockBits(new Rectangle(0, 0, _bitmap.Width, _bitmap.Height), ImageLockMode.ReadWrite, _bitmap.PixelFormat);
int noOfBitsNeededForStorage = _bitmap.Width * _bitmap.Height;
_imageData = new byte[noOfBitsNeededForStorage * ColorDepth / 8];
IntegerPointer = _bitmapData.Scan0;
Marshal.Copy(IntegerPointer, _imageData, 0, _imageData.Length);
throw new Exception("Bitmap is already locked.");
Marshal.Copy(_imageData, 0, IntegerPointer, _imageData.Length);
_bitmap.UnlockBits(_bitmapData);
throw new Exception("Bitmap is not locked.");
public class BitmapLockerTest
private static string path = @"locker.png";
Bitmap image = Grayscale.ToGrayscale(Bitmap.FromFile(path) as Bitmap);
Bitmap canvas = Grayscale.CreateGrayscaleImage(1024, 256);
BitmapLocker imageLocker = new BitmapLocker(image);
BitmapLocker canvasLocker = new BitmapLocker(canvas);
int startX = (Math.Abs(canvas.Width - image.Width) / 2);
int startY = (Math.Abs(canvas.Height - image.Height) / 2);
for (int y = startY; y < (startY + image.Height); y++)
for (int x = startX; x < (startX + image.Width); x++)
canvasLocker.SetPixel(x, y, imageLocker.GetPixel(xxx, yyy));
new PictureBoxForm(canvas, image).ShowDialog();