How can i get a color under the cursor in Silverlight?
I'm trying to make a simple color picker based on a picture like this. And so i need to get color under the cursor, how can i get 开发者_开发技巧it? All i have found about this was about a desktop applications in C#, but not about a silverlight way. Thank you.
You cant access the raw image data directly in silverlight, so you'll need to keep your data into a byte array, add a handler for MouseMove
,or MouseLeftButtonDown
to get the X/Y position of the cursor, and from that you can compute what values to read from your byte array, and from that determine your color.
a easier/better way would be to define the algorithm you use to generate your colour picker, and just use it directly to calculate the RGB value you need from the X/Y inputs. ie in your example there, hue is changing in respect to the X value, and lightness with respect to Y.
Are you generating that image yourself? Or do you want this to work with any arbitrary image?
If you need to do it for an arbitrary image, you will want to render the image into a WriteableBitmap. Use MouseEventArgs.GetPosition() to get the location of the mouse relative to the image element. Access the Pixels array data on the WriteableBitmap and convert the x/y coordinates of the mouse cursor into an index into the array.
精彩评论