FireStorm FireStorm - 1 month ago 16
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)

{
base.OnCreate(bundle);


SetContentView(Resource.Layout.Main);

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

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

imageView1.SetImageBitmap(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

Needed to use

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