简体   繁体   中英

.Net Maui - Create ImageSource from pixel data

I have an twodimensional array which holds data for each pixel of an image. What I'm trying to do is to visualize this data in an.Net Maui (XAML).

My approach would be <Image/> where i bind the ImageSource property to my data and to write a custom IValueConverter . But that's where I'm struggling. I can't find a way to write a custom image source which works for all platforms.

Another idea was to use a <Canvas/> and draw the data manually. But this has some quirks. On smaller image scales (10x10) the result gets pretty blurry and i can't find any way to disable the antialising. To prevent that i would have to draw larger rectangles per pixel. But this would add some unnecessary complexity where i have to calculate the actual width and height and use that to calculate the rectangle sizes.

So what is the simplest and best way to display pixel data in an MAUI App? To keep it simple we can say that we have a byte[,] (Greyscale) or a Color[,] array as data (whatever is simpler for the solution).

Hope this can help.

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {
        if (value == null)
            return null;

        var byteArray = getImageByteArray(value as string);

        return ImageSource.FromStream(() => new MemoryStream(byteArray));
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM