2012-01-06 16 views
3

画像を表示する画像ビューアがあります。画像上にマウスで矩形を描き、矩形のX座標とY座標(X1、X2、Y1、Y2)を取得します。これらの座標を使用して検索領域を作成し、配列内の最大値と最小値を両方の軸の画像と正確なピクセル数で検索します。
誰も私を始める方向に導くことができますか?矩形を使用して画像に検索領域を作成する

答えて

2

キャンバスを使用してイメージを表示し、その上に矩形を描画する必要があります。

例:

MainWindow.xaml:

<Window x:Class="CanvasRectangleSample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow"> 
    <Grid> 
     <Canvas x:Name="SampleImageCanvas" 
       MouseMove="SampleImageCanvas_MouseMove" 
       MouseDown="SampleImageCanvas_MouseDown" 
       Width="512" Height="389"> 
      <Canvas.Background> 
       <!--Here you set the image to display -> You probably want to bind it to something. --> 
       <ImageBrush x:Name="SampleImage" Stretch="Uniform" ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"> 
       </ImageBrush> 
      </Canvas.Background> 
      <!-- Here you draw whatever you want on the canvas. --> 
      <!-- You'll probably want to bind its width and height to something too. --> 
      <Rectangle x:Name="ROI" Stroke="#FFF1133E" Width="50" Height="50"/> 
     </Canvas> 
    </Grid> 
</Window> 

MainWindow.xaml.cs:あなたがいる場合にキャンバス領域に四角形の再配置を制限することができますしたい場合は

using System.Windows; 
using System.Windows.Input; 
using System.Windows.Controls; 
namespace CanvasRectangleSample 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      this.DataContext = this; 
      InitializeComponent(); 
     } 

     // Handling the redrawing of the rectangle according to mouse location 
     private void SampleImageCanvas_MouseMove(object sender, MouseEventArgs e) 
     { 
      //get mouse location relative to the canvas 
      Point pt = e.MouseDevice.GetPosition(sender as Canvas); 

      //here you set the rectangle loction relative to the canvas 
      Canvas.SetLeft(ROI, pt.X - (int)(ROI.Width/2)); 
      Canvas.SetTop(ROI, pt.Y - (int)(ROI.Height/2)); 
     } 


     private void SampleImageCanvas_MouseDown(object sender, MouseButtonEventArgs e) 
     { 
      //Here you should handle saving the rectangle location 

      //don't forget to calculate the proportion between Canvas's size and real Image's size. 

     } 
    } 

} 

キャンバス領域にマウスのロケーションが含まれているかどうかをチェックする式

1

ありがとうtersとhelp: これは私の完成したコードであり、動作します。キャンバス上の任意の場所にマウスを置くと、マウスを押し下げたままドラッグすると、矩形が作成されます。任意の方向に長方形をドラッグして作成するには、さらに改善が必要です。

XAML:

<Canvas Name="ImageCanvas" 
      MouseMove="ImageCanvas_MouseMove" 
       MouseDown="ImageCanvas_MouseDown" 
      Height="240" Width="320" 
      HorizontalAlignment="Left" 
      Margin="87,514,0,0" VerticalAlignment="Top" MouseLeftButtonUp="ImageCanvas_MouseLeftButtonUp"> 
      <Canvas.Background> 
      <ImageBrush x:Name="Image" Stretch="Uniform" ImageSource="C:\image.bmp"> 
      </ImageBrush> 
      </Canvas.Background> 
      <Rectangle x:Name="ROI" Stroke="#FFF1133E" Width="20" Height="20" Canvas.Left="155" Canvas.Top="115" /> 

     </Canvas> 

コード:

double topLeftX = 0; 
double topLeftY = 0; 
double bottomRightX = 0; 
double bottomrigthY = 0; 
bool setRect = false; 

private void ImageCanvas_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    topLeftY = topLeftX = bottomrigthY = bottomRightX = 0; 
    setRect = true; 
    System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas); 
    topLeftX = pt.X; topLeftY = pt.Y; 
} 

private void ImageCanvas_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    if (setRect == true) 
    { 
     //get mouse location relative to the canvas 
     System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas); 
     Canvas.SetLeft(ROI, topLeftX); 
     Canvas.SetTop(ROI, topLeftY); 
     ROI.Width = System.Math.Abs((int)(pt.X - topLeftX)); 
     ROI.Height = System.Math.Abs((int)(pt.Y - topLeftY)); 
     commandReturnTB.Text = (Convert.ToString(pt.X) + "," + Convert.ToString(pt.Y))+"\n"; 
    } 
} 

private void ImageCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    System.Windows.Point pt = e.MouseDevice.GetPosition(sender as Canvas); 
    bottomRightX = pt.X; 
    bottomrigthY = pt.Y; 
    ROI.Width = System.Math.Abs((int)(bottomRightX - topLeftX)); 
    ROI.Height = System.Math.Abs((int)(bottomrigthY - topLeftY)); 
    Canvas.SetLeft(ROI, topLeftX); 
    Canvas.SetTop(ROI, topLeftY); 
    setRect = false; 
    commandReturnTB.Text = topLeftX + "," + topLeftY + "--" + bottomRightX + "," + bottomrigthY; 
} 
+0

こんにちはユーザーは、私はあなたの解決策を見つけたうれしいです。私は私の答えが役に立つと思った、あなたのソリューションはそれに基づいているので、このスレッドの答えとして私の答えをマークしてください。 – Seffix

関連する問題