Grayscale

This algorithm converts input image to grayscale (a.k.a greyscale) image.

   



									Public Shared Sub ApplyGrayscale(ByRef bmp As Bitmap)
	Dim bmData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
	Dim p As IntPtr = bmData.Scan0
	Dim stopAddress As Integer = CInt(p) + bmData.Stride * bmData.Height

	While CInt(p) <> stopAddress
		Dim gs = Math.Truncate(0.299 * Marshal.ReadByte(p + 2) + 0.587 * Marshal.ReadByte(p + 1) + 0.114 * Marshal.ReadByte(p))
		Marshal.WriteByte(p, gs)
		Marshal.WriteByte(p + 1, gs)
		Marshal.WriteByte(p + 2, gs)
		p += 3
	End While

	bmp.UnlockBits(bmData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyGrayscale(b)