在Xamarin.Forms中从Tapped Image Source中删除像素颜色

pcrecxhr  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(88)

我正在做一个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);
}

s2j5cfk0

s2j5cfk01#

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

字符串
在这段代码中,我使用picturebox作为你的Image,然后在picturebox.Source中的所有像素的每个RGB中添加了一个数字10。

相关问题