Friday, January 2, 2009

Image Processing I : Invert Colors

Hi all,

I'm starting with a basic image processing algorithm. After a while, the whole solution and sample files will be published. I'm also developing this library and sharing with you. Anyway, you can see the algorithm below. All basic colors are converted by subtracting from 255 and founded new Red, Green and Blue color codes. All converted pixel is set to a new image instance. See you at harder image processing algorithms later. Let's See;


'--------------------Code
--------------------
Public Shared Function InvertColors(ByVal img As Image) As Image
Dim rslt As System.Drawing.Image
Dim btmpImg, btmpRslt As New Bitmap(img.Width, img.Height)
btmpImg = DirectCast(img, Bitmap)
Dim rC, gC, bC As Integer
For i As Integer = 0 To btmpImg.Width - 1
For j As Integer = 0 To btmpImg.Height - 1
rC = 255 - btmpImg.GetPixel(i, j).R
gC = 255 - btmpImg.GetPixel(i, j).G
bC = 255 - btmpImg.GetPixel(i, j).B
btmpRslt.SetPixel(i, j, Color.FromArgb(rC, gC, bC))
Next
Next
rslt = DirectCast(btmpRslt, Image)
Return rslt
End Function

'--------------------Code--------------------

No comments: