vb.net Code:
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = GetGreyscaledImage(New Bitmap("C:\Documents and Settings\Fromethius\My Documents\My Pictures\image.png"))
End Sub
Public Shared Function GetGreyscaledImage(ByRef setImage As Image) As Image
Dim greyscaleBitmap As Bitmap = New Bitmap(setImage.Width, setImage.Height)
Dim greyscaleGraphics As Graphics = Graphics.FromImage(greyscaleBitmap)
Dim greyscaleColorMatrix As New ColorMatrix(New Single()() { _
New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
New Single() {0.5F, 0.5F, 0.5F, 0, 0}, _
New Single() {0, 0, 0, 1, 0, 0}, _
New Single() {0, 0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 0, 1}})
Dim greyscaleImageAttributes As New ImageAttributes()
greyscaleImageAttributes.SetColorMatrix(greyscaleColorMatrix)
greyscaleGraphics.DrawImage(setImage, New Rectangle(0, 0, setImage.Width, setImage.Height), 0, 0, setImage.Width, setImage.Height, GraphicsUnit.Pixel, greyscaleImageAttributes)
Return greyscaleBitmap
End Function
End Class