2010-12-20 12 views
8

मेरे पास एक चमकदार पृष्ठभूमि के साथ नियंत्रण है। MouseDown या MouseUp ईवेंट पर मैं कैप्चर करना चाहता हूं कि माउस पॉइंटर के नीचे पिक्सेल कितना रंग है। मुझे यह कैसे करना है?डब्ल्यूपीएफ: सूचक के तहत रंग

उत्तर

4

मैंने एक ऐसा व्यवहार बनाया जो रंग को पकड़ने के लिए छवि ऑब्जेक्ट से जुड़ा जा सकता है, जो 100% WPF है। व्यवहार यहाँ है; इसे किसी भी छवि के साथ किसी भी "दृश्य" के साथ काम करने के लिए tweaked किया जा सकता है।

[नोट: मैं RenderTargetBitmap के निर्माण के लिए हार्डकोडेड 96dpi ... अपने लाभ भिन्न हो सकते]

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]); 
      } 
     }   
     }   
    } 
+0

कि अच्छा है, लेकिन कैसे और साथ ही अल्फा चैनल मूल्य प्राप्त करने के? –

+0

ऐसा लगता है कि यह 'एसोसिएटेड ऑब्जेक्ट' प्रस्तुत करता है जो माउस के नीचे होने वाला नहीं होता है। – Maslow

संबंधित मुद्दे