Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Check trannsparency of a given pixel in Image using C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.61k
    Comment on it

    Many a times we come across to situations where we need to find out whether a given pixel in an image is a solid pixel or a Transparent Pixel.

    Here is how we can check that.

    We have an image property called alpha value which tells us about the transparency of image. A pixel can be of three types depending upon its alpha value:

    1. Opaque = 255(if Alpha value is 255)
    2. Translucent = 1-254(if Alpha value between 0-254)
    3.Transparent = 0(if Alpha value is 0)

    Here is the short C# program which identifies the transparency of image on above mentioned alpha values:

            int xPixel = 50;//Xpixel of point for which we want to check transparency
            int yPixel = 50;//Ypixel of point for which we want to check transparency
    
            int transparent = 0;//Alpha code of translucent is 0
            int translucent = 254;//Alpha code of translucent is 1-254
    
            Bitmap bitmapInstance = (Bitmap)Image.FromFile("C:/Users/User/Desktop/Test.png");//Get Image Instance
            byte alphaValue = bitmapInstance.GetPixel(xPixel, yPixel).A;//Get alpha value of Point
    
            if (alphaValue == transparent)
                MessageBox.Show("Pixel is Transparent");
            else if (alphaValue <= translucent)
                MessageBox.Show("Pixel is Translucent");
            else
                MessageBox.Show("Pixel is Opaque");//>254 is Opaque
    

    Using the above code we can identify the alpha value and transparency of pixel

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: