FireStorm FireStorm - 10 months ago 72
Android Question

Unable to read pixels from bitmap using Xamarin

I thought trying to get pixel data would be a simple thing to do, but it has become a big headache.

My original goal is to go through every pixel in the bitmap and then do some modification with the data. However every time I try to get the pixel I get 0 as a result. It feels like the image is not going to the bitmap. Am I missing something in my code or the code in general wrong?

protected override void OnCreate(Bundle bundle)



var imageView1 = FindViewById<ImageView>(Resource.Id.imageView1);

Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.pic1);
b = Bitmap.CreateBitmap(b);


for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)

int pixel = b.GetPixel(i, j);

int A = Color.GetAlphaComponent(pixel);
int R = Color.GetRedComponent(pixel);
int G = Color.GetGreenComponent(pixel);
int B = Color.GetBlueComponent(pixel);

Answer Source

Needed to use

Bitmap b = BitmapFactory.DecodeResource(Resources,Resource.Drawable.pic1);