WPF:指针下的颜色

Jon*_*len 8 wpf

我有一个渐变背景的控件.在MouseDownMouseUp事件上,我想要捕获像素在鼠标指针下是什么颜色.我该怎么办?

Alb*_*eld 6

我创建了一个可以附加到 Image 对象以获取颜色的行为,即 100% WPF。这是行为;它可以进行调整以适应任何“视觉”,而不仅仅是图像。

[注意:我硬编码 96dpi 来创建 RenderTargetBitmap...您的情况可能会有所不同]

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]);
            }
         }        
      }        
   }
Run Code Online (Sandbox Code Playgroud)