3
votes

I want to get the Color of a pixel in a Bitmap. For that normally I use GetPixel(x,y). But with Android.Graphics that method gives me an int representing the Color. So I need to know how can I get this Color from that integer.

In fact that is what I want to do finaly (a white removal in the mPlan):

for (int x=0; x <  PlanWidth; x++)
{
    for (int y=0; y <  PlanHeight; y++)
    {
        if (mPlan.GetPixel(x, y) ==  Color.White)                    
            mPlan.SetPixel(x, y, (Color.White - mPlan.GetPixel(x, y)));
    }
}
2
Is something wrong with the current code? In java it will be completely correct, because Color.White is intSergey Glotov
What do you mean by "a white removal" ? Do you want to remove each white pixel from your bitmap? Or do you just want to darken each pixel ?yan yankelevich
Which color are you using? ie system.drawing.color, windows.media.color .MikeT
Color.White.ToArgb()? You can convert Color.White to int instead of each GetPixel() resultSergey Glotov
What returns GetPixel() if not an ARGB? I'm looking in the docs and it says "the returned color is a non-premultiplied ARGB value."Sergey Glotov

2 Answers

3
votes

Android.Graphics.Color has constructor Color(Int32). To convert int to Color you can do something like this:

new Color(mPlan.GetPixel(x, y)) ==  Color.White

but I think will be better to convert Color.White to int using Color.White.ToArgb() and replace SetPixel() argument by Color.Black because Color.White - Color.White will be Color.Black.

int white = Color.White.ToArgb();
for (int x=0; x < PlanWidth; x++)
{
    for (int y=0; y < PlanHeight; y++)
    {
        if (mPlan.GetPixel(x, y) ==  white)                    
            mPlan.SetPixel(x, y, Color.Black);
    }
}
1
votes

To cast your int color to RGB color in xamarinC# for android you just have to do this :

Color myColor = new Color(yourBitmap.GetPixel(x,y);

Then you can work with it's components like this :

 byte myColorRedValue = myColor.R;
 byte myColorBlueValue = myColor.B;
 byte myColorGreenValue = myColor.G;
 byte myColorAlphaValue = myColor.A;

So for example if you want to darken your color just do this :

 myColor.R = (byte)(myColor.R / 2);
 myColor.G = (byte)(myColor.G / 2);
 myColor.B = (byte)(myColor.B / 2);

This will give you three int between 0 and 255. To darken your color you just have to substract them with some number (obviously you have to check if your results are superiors or equals to zero).

This is the easiest way to achieve what you want and to understand how it works from a beginner perspective