I want to get the Pixel Color of an Image at the current pointer/mouse position in a universal app with C#. As the Bitmap Class does not work anymore, all the hints I found, were useless. The purpose of this is to program a color picker.
Asked
Active
Viewed 123 times
1 Answers
1
Using an image that's a resource in the project, this seems to work (using WriteableBitmapEx)
WriteableBitmap wb;
private async void ImageOpened(object sender, RoutedEventArgs e)
{
wb = await BitmapFactory.New(1, 1).FromContent(((sender as Image).Source as BitmapImage).UriSource);
}
private async void ImageTapped(object sender, TappedRoutedEventArgs e)
{
var pos = e.GetPosition(sender as UIElement);
var px = wb.GetPixel((int)pos.X, (int)pos.Y);
System.Diagnostics.Debug.WriteLine($"R: {px.R} G: {px.G} B: {px.B} ");
}

Matt Lacey
- 65,560
- 11
- 91
- 143
-
Thank you very much. I already had tried the WriteableBitmap class, but Visual Studio 2015 marked the GetPixel method as not defined. In the Microsoft documentation I didn't find it either. Your remark (using WirteableBitmapEx) was very helpful. Now I have installed the WirteableBitmapEx package and the method is available. – Andi_S Jan 03 '17 at 13:42
-
-
@Lacey If I use BitmapImage.SetSource from a stream so I cant use the UriSource.How can I convert BitmapImage to writeableBitmap? – lindexi Jan 05 '17 at 06:27
-