Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Color under the pointer

Tags:

wpf

I have a control with a gradiant background. On the MouseDown or MouseUp event I want to capture what color the pixel is immeidately under the mouse pointer. How would I do that?

like image 890
Jonathan Allen Avatar asked Dec 20 '10 17:12

Jonathan Allen


1 Answers

I created a behavior that can be attached to an Image object to grab the color, which is 100% WPF. Here is the behavior; it could be tweaked to work with any "Visual", not just an Image.

[Note: I hardcoded 96dpi for the creation of RenderTargetBitmap... your milage may vary]

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Company.Solution.Project.Utilities.Behaviors
{
    [Description("Used to sample the color under mouse for the image when the mouse is pressed. ")]
    public class ImageBehaviorMouseDownPointSampleToColor : Behavior<Image>
    {
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Color),
                                        typeof(ImageBehaviorMouseDownPointSampleToColor),
                                        new UIPropertyMetadata(Colors.White));


        public Color SelectedColor
        {
            get { return (Color)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }


        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseMove += AssociatedObject_MouseMove;
            AssociatedObject.MouseDown += AssociatedObject_MouseDown;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            AssociatedObject.MouseMove -= AssociatedObject_MouseMove;
            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
        }

        private void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SamplePixelForColor();
        }

        private void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                SamplePixelForColor();
            }
        }

        private void SamplePixelForColor()
        {
            // Retrieve the coordinate of the mouse position in relation to the supplied image.
            Point point = Mouse.GetPosition(AssociatedObject);

            // Use RenderTargetBitmap to get the visual, in case the image has been transformed.
            var renderTargetBitmap = new RenderTargetBitmap((int)AssociatedObject.ActualWidth,
                                                            (int)AssociatedObject.ActualHeight,
                                                            96, 96, PixelFormats.Default);
            renderTargetBitmap.Render(AssociatedObject);

            // Make sure that the point is within the dimensions of the image.
            if ((point.X <= renderTargetBitmap.PixelWidth) && (point.Y <= renderTargetBitmap.PixelHeight))
            {
                // Create a cropped image at the supplied point coordinates.
                var croppedBitmap = new CroppedBitmap(renderTargetBitmap,
                                                      new Int32Rect((int)point.X, (int)point.Y, 1, 1));

                // Copy the sampled pixel to a byte array.
                var pixels = new byte[4];
                croppedBitmap.CopyPixels(pixels, 4, 0);

                // Assign the sampled color to a SolidColorBrush and return as conversion.
                SelectedColor = Color.FromArgb(255, pixels[2], pixels[1], pixels[0]);
            }
         }        
      }        
   }
like image 100
Albert Oldfield Avatar answered Nov 04 '22 05:11

Albert Oldfield