我正在做一个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);
}
型
1条答案
按热度按时间s2j5cfk01#
字符串
在这段代码中,我使用picturebox作为你的
Image
,然后在picturebox.Source
中的所有像素的每个RGB中添加了一个数字10。