Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Convert invalid – out of range RGB value into valid RGB value in -

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how we can convert invalid RGB value into valid RGB range value in -

 Sometimes when we are performing different operations on image especially when we are applying different filter masks on an image, the RGB values get invalid. Sometimes RGB value become less than zero or sometimes it becomes greater than 255. In this case C# gives an exception that values is out of range of the RGB values.

We can handle this problem by the following method.

We have three components in a color red, green and blue. First take a blue component convert it into long and then shift it left 16 times. After shifting the left now convert it into byte. Then take green component convert it into long shift it left 8 times. After shifting left convert it into byte type. Then take red component and convert it into byte.

After performing the above operation take or of all the R, G and B component and convert it into integer value that will be the valid RGB value.

To demonstrate make a window application. Drag three text boxes and a button on form.

Now write the following code on Button click event:


private void btn_Convert_Click(object sender, EventArgs e)


            int red = 0;

            int green = 0;

            int blue = 0;

            string rd = txt_R.Text;

            string gn = txt_G.Text;

            string bl = txt_B.Text;

            red = Convert.ToInt32(rd);

            green = Convert.ToInt32(gn);

            blue = Convert.ToInt32(bl);

            long r1 = Convert.ToInt64(red);

            long g1 = Convert.ToInt64(green);

            long b1 = Convert.ToInt64(blue);

            g1 = Convert.ToInt64(g1 << 8);

            b1 = Convert.ToInt64(b1 << 16);

            int ans = Convert.ToInt16(((byte)r1 | (byte)g1 | (byte)b1));




Private Sub btn_Convert_Click(ByVal sender As ObjectByVal As EventArgs)
Dim red As Integer = 0
Dim green As Integer = 0
Dim blue As Integer = 0
Dim rd As String = txt_R.Text
Dim gn As String = txt_G.Text
Dim bl As String = txt_B.Text
Dim r1 As Long = Convert.ToInt64(red)
Dim g1 As Long = Convert.ToInt64(green)
Dim b1 As Long = Convert.ToInt64(blue)
Convert.ToInt64((g1 + 8))
Convert.ToInt64((b1 + 16))
Dim ans As Integer = Convert.ToInt16((CType(r1,Byte)  _
Or (CType(g1,ByteOr CType(b1,Byte))))
End Sub

This is simple code to convert invalid range RGB value into valid RGB value.

 Now write the following code on FORM LOAD event:


private void Form1_Load(object sender, EventArgs e)


                this.Text = "DEVASP RGB APPLICATION";


Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)

End Sub

This simple article tells that how we can convert invalid RGB value into valid RGB range value in -

Add Article Comment:
Name :
Email Address :
Comments :
<< Remove noise from image using median filter in -

Disclaimer - Privacy
© 2002-2017