Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:24:53+00:00 2026-05-25T11:24:53+00:00

I have a control with a gradiant background. On the MouseDown or MouseUp event

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:24:54+00:00Added an answer on May 25, 2026 at 11:24 am

    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]);
                }
             }        
          }        
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to let a group box control the background color for a radio
I want to read UTF-8 strings from a server that I have control of,
I want to have control over my Http Exceptions to the point of being
I have control with custom drawings, let's call it Surface . I want to
My application is shipped to many different customers, who all want to have control
Ok so I have a custom control and I am applying a background gradient
I have control, subclassed from CDialogBar, it has some buttons(like on toolbar). When I
Some code that I don't have control over is overriding the global JSON object
In WinForms, you have Control.BeginInvoke() , which means you can marshall a call from
First, I'll start off by saying that I do not have control over the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.