How to create a Gray scale image From a Color Image .net coding

Sunday, 7 December 2014

How to create a Gray scale image From a Color Image .net coding

The Below code is to convert a color image to gray scale image  



 Public Shared Function MakeGrayscale(ByVal original As Bitmap) As Bitmap
        'make an empty bitmap the same size as original
        Dim newBitmap As New Bitmap(original.Width, original.Height)

        For i As Integer = 0 To original.Width - 1
            For j As Integer = 0 To original.Height - 1
                'get the pixel from the original image
                Dim originalColor As System.Drawing.Color = original.GetPixel(i, j)

                'create the grayscale version of the pixel
                Dim grayScale As Integer = CInt(Math.Truncate((originalColor.R * 0.3) + (originalColor.G * 0.59) + (originalColor.B * 0.11)))

                'create the color object
                Dim newColor As System.Drawing.Color = System.Drawing.Color.FromArgb(grayScale, grayScale, grayScale)

                'set the new image's pixel to the grayscale version
                newBitmap.SetPixel(i, j, newColor)
            Next
        Next

        Return newBitmap
    End Function