2013-02-14 48 views
0

我有一個項目mvvm。我通過點擊我的項目中的加載按鈕來以.jpg格式加載圖像。我想選擇這個形象的一部分,移動它,並重新它的大小,例如在我的菜單矩形工具漆通過點擊,但我不知道如何wpf mvvm ImageSelector

視圖代碼:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="240*" /> 
     <RowDefinition Height="60*" /> 
    </Grid.RowDefinitions> 
    <StackPanel Orientation="Horizontal" Grid.Row="0"> 
     <Image Source="{Binding MyImage.Source}" ></Image> 
    </StackPanel> 
    <StackPanel Orientation="Horizontal" Grid.Row="1"> 
     <Button Width="70" Height="40" Command="{Binding LoadCommand}">Load Image</Button> 
    </StackPanel> 
</Grid> 

視圖模型代碼:

#region Property 
    private Image _MyImage; 
    public Image MyImage 
    { 
     get 
     { 
      return _MyImage; 
     } 
     set 
     { 
      _MyImage = value; 
      OnPropertyChanged("MyImage"); 
     } 
    } 
    #endregion 

    #region Constructor 
    public ImageViewModel() 
    { 
     _MyImage = new Image(); 
    } 
    #endregion 

    #region Commands 
    RelayCommand _LoadCommand; 
    public ICommand LoadCommand 
    { 
     get 
     { 
      if (_LoadCommand == null) 
      { 
       _LoadCommand = new RelayCommand(param => LoadCommandExecute()); 
      } 
      return _LoadCommand; 
     } 
    } 
    #endregion 

    #region Methods 
    private void LoadCommandExecute() 
    { 

     OpenFileDialog op = new OpenFileDialog(); 
     op.Title = "Select a picture"; 
     op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + 
      "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + 
      "Portable Network Graphic (*.png)|*.png"; 
     if (op.ShowDialog() == true) 
     { 
      MyImage.Source = new BitmapImage(new Uri(op.FileName)); 
     } 
    } 
    #endregion 

回答

0

實施Drag Selection是解決這個問題的方法之一。在執行拖動選擇後,請檢查選擇矩形與圖像的相對位置,剪出圖像的該部分並允許以任何您喜歡的方式進行修改。

如果您一路上遇到問題,請發佈一些代碼,我們會盡力解決它。