如何使用Wpf,MVVM获取图像像素位置和RGB值?

时间:2017-11-20 06:50:41

标签: c# wpf mvvm

我可以在mousemove上加载Image并获取像素位置,现在我想显示每个像素的RGB值。任何人都可以帮助我

MainWindowViewModel.cs

命名空间WpfAppTest.ViewModel {     class MainViewModel:ViewModelBase     {         private BitmapImage _image;         public BitmapImage Image         {             get {return _image; }             组             {                 _image = value;                 OnPropertyChanged("图像&#34);             }         }         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>

0 个答案:

没有答案