0

I have the following code in my Universal App (Windows Phone):

      void colourPicker_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        PicturePicker picturePicker = sender as PicturePicker;

        GeneralTransform transform = picturePicker.TransformToVisual(ImageHolder);
        Point controlPosition = transform.TransformPoint(new Point(0, 0));

        int pointX = (int)controlPosition.X;
        int pointY = (int)controlPosition.Y;

        Color c = writeableBmp.GetPixel(pointX, pointY);

        //  WriteableBitmap newWB = writeableBmp.Crop(pointX - 21, pointY - 21, 42, 42);
        //  picturePicker.SetImageBrush(newWB);


        SolidColorBrush brush = new SolidColorBrush(c);
        picturePicker.SetColor(brush);


        Canvas.SetLeft(picturePicker, Canvas.GetLeft(picturePicker) + e.Delta.Translation.X);
        Canvas.SetTop(picturePicker, Canvas.GetTop(picturePicker) + e.Delta.Translation.Y);
    }

I have a image, and on top of that a UserControl (very simple XAML) that the user can drag around to find the color he/she wants. Only dragging works great on ManipulationDelta, but when I try to GetPixel() everytime the ManipulationDelta event triggers the translate becomes very laggy. If I comment out the setting of the color (picturePicker.SetColor) it's still slow, so that's not the problem.

Weird enough, very similiar code worked fluid in the old version of this app in Silverlight (on the same phone).

Any thoughts on how to improve this?

Niels
  • 2,496
  • 5
  • 24
  • 38
  • Accessing pixels individually will always be slow. If you want it to be faster, you need to lock the bitmap and access the data directly (this includes interpreting the raw bytes yourself). You can call `Lock()` and then read the `BackBuffer` property to get the raw data. You'll need to check the `BackBufferStride` and pixel format to correctly interpret the raw bytes. – Peter Duniho Dec 27 '14 at 19:24
  • What Peter said. http://stackoverflow.com/questions/1563038/fast-work-with-bitmaps-in-c-sharp has the code for it. – Gabriel Dec 27 '14 at 19:28
  • Thanks for your comments, guys! I have checked the code, but I'm kinda new to this image processing stuff. Any chance you can help me with some applied code like written above? – Niels Dec 27 '14 at 19:55
  • Btw, WriteAbleBitmap.ToByteArray already gives me a ARGB array? – Niels Dec 27 '14 at 20:05
  • Is this for a color picker control? One with static image? – yasen Dec 27 '14 at 21:15
  • It's an Image (the source can be dynamic, inserted by the user). – Niels Dec 27 '14 at 22:57

0 Answers0