I am able to load Image and get pixel position on mousemove, Now I want to display RGB value of each pixel. Can any one help me
MainWindowViewModel.cs
namespace WpfAppTest.ViewModel { class MainViewModel: ViewModelBase { private BitmapImage _image; public BitmapImage Image { get { return _image; } set { _image = value; OnPropertyChanged("Image"); } } public MainViewModel() { _image = new BitmapImage(); _textBox = new TextBox(); }
private RelayCommand _loadImageCommand;
public ICommand LoadImageCommand
{
get
{
if (_loadImageCommand == null)
{
_loadImageCommand = new RelayCommand(param => LoadImage());
}
return _loadImageCommand;
}
}
private void LoadImage()
{
OpenFileDialog op = new OpenFileDialog();
op.Title = "Select a Picture";
op.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" + "All files (*.*)|*.*";
op.Multiselect = true;
if (op.ShowDialog() == true)
{
Image = new BitmapImage(new Uri(op.FileName));
}
}
private RelayCommand _mouseMoveCommand;
public RelayCommand MouseMoveCommand
{
get
{
if (_mouseMoveCommand == null) return _mouseMoveCommand = new RelayCommand(param => ExecuteMouseMove((MouseEventArgs)param));
return _mouseMoveCommand;
}
set { _mouseMoveCommand = value; }
}
private void ExecuteMouseMove(MouseEventArgs e)
{
System.Windows.Point p = e.GetPosition(((IInputElement)e.Source));
TextBox.Text = String.Format("X: {0} Y:{1}", (int)p.X, (int)p.Y);
}
private TextBox _textBox;
public TextBox TextBox
{
get { return _textBox; }
set
{
_textBox = value;
OnPropertyChanged("TextBox");
}
}
}
}
Here in :
private void ExecuteMouseMove(MouseEventArgs e)
{
System.Windows.Point p = e.GetPosition(((IInputElement)e.Source));
TextBox.Text = String.Format("X: {0} Y:{1}", (int)p.X, (int)p.Y);
}
I am able to get pixel position, now i want rgb value of pixels
MouseBehaviour:
public class MouseBehaviour
{
public static readonly DependencyProperty MouseMoveCommandProperty =
DependencyProperty.RegisterAttached("MouseMoveCommand", typeof(ICommand), typeof(MouseBehaviour), new FrameworkPropertyMetadata(new PropertyChangedCallback(MouseMoveCommandChanged)));
private static void MouseMoveCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)d;
element.MouseMove += new MouseEventHandler(element_MouseMove);
}
static void element_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
ICommand command = GetMouseMoveCommand(element);
command.Execute(e);
}
public static void SetMouseMoveCommand(UIElement element, ICommand value)
{
element.SetValue(MouseMoveCommandProperty, value);
}
public static ICommand GetMouseMoveCommand(UIElement element)
{
return (ICommand)element.GetValue(MouseMoveCommandProperty);
}
}
MainWindow.xaml:
<Window.Resources>
<vm:MainViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*" />
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Menu FontSize="20">
<MenuItem Header="File">
<MenuItem Header="Open" Command="{Binding LoadImageCommand}"/>
<MenuItem Header="Reload"/>
<MenuItem Header="Save"/>
<MenuItem Header="Exit"/>
</MenuItem>
<MenuItem Header="Filters">
<MenuItem Header="Gray"/>
<MenuItem Header="Canny"/>
<MenuItem Header="Sobel"/>
<MenuItem Header="Laplacian"/>
</MenuItem>
</Menu>
</Grid>
<Grid Grid.Row="1">
<Border BorderThickness="1" BorderBrush="Pink">
<Viewbox Margin="5,5,5,5">
<viewers:Viewer>
<Image x:Name="img" Source="{Binding Image}"
Model:MouseBehaviour.MouseMoveCommand="{Binding MouseMoveCommand}">
</Image>
</viewers:Viewer>
</Viewbox>
</Border>
</Grid>
<Grid Grid.Row="2">
<StackPanel Orientation="Horizontal">
<TextBox Focusable="False" Text="{Binding TextBox.Text}" Width="100"/>
<TextBox Focusable="False" Width="110"/>
</StackPanel>
</Grid>
</Grid>