从 Xamarin.Forms 中点击的图像源检索像素颜色

问题描述 投票:0回答:1

我正在开发一个 Xamarin.Forms 项目,并且希望从直接在 XAML 中设置为源的图像中检索像素的颜色。用户交互是通过点击图像来触发的。我探索了使用 TapGestureRecognizer 和 DependencyService 来实现特定于平台的实现。

XAML:

<Image x:Name="image" Source="your_image_source.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding OnImageTappedCommand}" />
    </Image.GestureRecognizers>
</Image>

C#(Xamarin.Forms):

public partial class YourPage : ContentPage
{
    public YourPage()
    {
        InitializeComponent();

        image.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = new Command(OnImageTapped)
        });
    }

    private void OnImageTapped()
    {
        // Need guidance on how to retrieve the pixel color here
        // using DependencyService or any other suitable method.
    }
}

特定于平台的接口(IPixelColorService):

public interface IPixelColorService
{
    Color GetPixelColor(object imageSource, int x, int y);
}

特定于平台的实现(Android):

[assembly: Dependency(typeof(PixelColorService))]

namespace YourNamespace.Droid
{
    public class PixelColorService : IPixelColorService
    {
        public Color GetPixelColor(object imageSource, int x, int y)
        {
            // Implement platform-specific code to get the pixel color at (x, y) in the image
            // ...

            // For now, just returning a random color for demonstration purposes
            return Color.FromRgb(255, 0, 0);
        }
    }
}

我正在寻求有关如何实现 OnImageTapped 方法以从图像中的点击位置检索像素颜色的指导,其中图像源直接在 XAML 中设置。我想使用 DependencyService 或任何其他合适的方法。任何代码示例或建议将不胜感激。

编辑:

我的 c# winforms 代码:

((Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y)

我的 C# 代码隐藏:

public SKColor GetPixel(int x, int y);
private void mousemove(object sender, PanUpdatedEventArgs e)
{
    return GetPixel(e.X, e.Y);
}
c# image xamarin xamarin.forms pixel
1个回答
1
投票
        private async void pixels()
        {
            Stream imageStream = null;
            if (picturebox.Source is FileImageSource fileImageSource)
            {
                string filePath = fileImageSource.File;
                imageStream = File.OpenRead(filePath);
            }
            else if (picturebox.Source is UriImageSource uriImageSource)
            {
                using (HttpClient client = new HttpClient())
                {
                    byte[] imageDate = await client.GetByteArrayAsync(uriImageSource.Uri);
                    imageStream = new MemoryStream(imageDate);
                }
            }
            byte[] imageData = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                await imageStream.CopyToAsync(memoryStream);
                imageData = memoryStream.ToArray();
            }
            for (int i = 0; i < imageData.Length; i += 4)
            {
                imageData[i] = (byte)Math.Min(255, imageData[i] + 10); // Blue
                imageData[i + 1] = (byte)Math.Min(255, imageData[i + 1] + 10); // Green
                imageData[i + 2] = (byte)Math.Min(255, imageData[i + 2] + 10); // Red and [i + 3] for Alpha
            }
            using (MemoryStream manipulatedStream = new MemoryStream(imageData))
            {
                picturebox.Source = ImageSource.FromStream(() => manipulatedStream);
            }
        }

在此代码中,我使用图片框作为您的

Image
,然后将数字10添加到
picturebox.Source
中所有像素的每个RGB。

© www.soinside.com 2019 - 2024. All rights reserved.