-1

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.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Andi_S
  • 1

1 Answers1

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
  • WriteableBitmapEx not WirteableBitmapEx . – lindexi Jan 04 '17 at 14:00
  • @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
  • Thx @Lacey .I've been found it for 10 days. – lindexi Jan 07 '17 at 01:16