Xamarin 中的未知成员 Bitmap.SetPixel(x, y, color)

Unknwon Member Bitmap.SetPixel(x, y, color) in Xamarin


我正在使用 Native Shared 项目开发 Xamarin 应用程序。
这是我的位图反转过滤器方法

using System;
using Android.Graphics;

public static Bitmap Inversion (Bitmap bmp) {

        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                var pixel = new Color(bmp.GetPixel(x, y));
                bmp.SetPixel(x, y, Color.Rgb(255 - pixel.R, 255 - pixel.G, 255 - pixel.B));
            }
        }
        return bmp;
    }

我收到一个 Java.Lang.IllegalStateException 错误,在对位图应用滤镜时,我不知道如何修复它,这是它发生的地方:

我知道这是一些 Xamarin 错误,无法识别 .SetPixel() 方法,我不知道为什么会这样。

这是像素变量的内容:

请帮忙

你的 Bitmap 是不可变的,因此你得到 IllegalStateException,你可以复制它,然后在副本上使用 SetPixel

public static Bitmap Inversion(Bitmap bmp)
{
    var mutableBitmap = Bitmap.CreateBitmap(bmp.Width, bmp.Height, bmp.GetConfig());
    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < bmp.Height; y++)
        {
            var pixel = new Color(bmp.GetPixel(x, y));
            var color = Color.Rgb(255 - pixel.R, 255 - pixel.G, 255 - pixel.B);
            mutableBitmap.SetPixel(x, y, color);
        }
    }
    return mutableBitmap;
}