Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / WPF

WPF Image Pixel Color Picker Element

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
30 May 2009CPOL6 min read 84.4K   3.8K   28  
WPF element to pick up an image (either bitmap or drawing) pixel color.
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace ImageColorPickerSample2
{
	/// <summary>
	/// Converts the Color value to the SolidColorBrush.
	/// </summary>
	[ValueConversion(/*sourceType*/ typeof(Color), /*targetType*/ typeof(Brush))]
	public class ColorToBrushConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Converts an Integer value to Visibility.
		/// </summary>
		/// <param name="value">The value produced by the binding source.</param>
		/// <param name="targetType">The type of the binding target property.</param>
		/// <param name="parameter">Provides the value to compare with the <paramref name="value"/>.
		/// Can be <c>null</c>.</param>
		/// <param name="culture"><see cref="M:System.Convert.ToInt64"/> convertion culture.</param>
		/// <returns>
		/// </returns>
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (targetType != typeof(Brush))
				return value;
			if (value == null || value == DependencyProperty.UnsetValue
				|| value.GetType() != typeof(Color))
				return Brushes.Transparent;
			Color color = (Color)value;
			return new SolidColorBrush(color);
		}

		/// <summary>
		/// Converts a value.
		/// </summary>
		/// <param name="value">The value that is produced by the binding target.</param>
		/// <param name="targetType">The type to convert to.</param>
		/// <param name="parameter">The converter parameter to use.</param>
		/// <param name="culture">The culture to use in the converter.</param>
		/// <returns>
		/// A converted value. If the method returns null, the valid null value is used.
		/// </returns>
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion IValueConverter Members
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions