无法使用Xamarin从位图读取像素

时间:2015-07-19 06:00:16

标签: c# android bitmap xamarin

我认为尝试获取像素数据是一件很简单的事情,但它已经成为一个令人头痛的问题。

我的最初目标是遍历位图中的每个像素,然后对数据进行一些修改。但是每次我尝试获取像素时,我都会得到0。感觉就像图像没有进入位图。我错过了我的代码或代码中的一些错误吗?

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);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

需要使用

{{1}}

答案 1 :(得分:0)

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            Color pixel = new Color(b.GetPixel(i, j));

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