【问题标题】:Using a rectangle to create a search region on an image使用矩形在图像上创建搜索区域
【发布时间】:2012-01-07 13:15:44
【问题描述】:

我有一个显示图像的图像查看器。我想在图像上使用鼠标绘制一个矩形并获取矩形的 x 和 y 坐标(X1、X2、Y1 和 Y2)。我将使用这些坐标创建一个搜索区域,并在一个数组中找到最大值和最小值,它们的像素数与两个轴上的图像的确切数量相同。
任何人都可以指导我开始的方向吗?

【问题讨论】:

    标签: c# wpf drawing


    【解决方案1】:

    您应该使用画布来显示图像并在其上绘制一个矩形。

    例子:

    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.
    
            }
        }
    
    }
    

    如果您愿意,可以使用 if 表达式检查画布区域是否包含鼠标位置,从而将矩形重定位限制在画布区域

    【讨论】:

      【解决方案2】:

      感谢指点和帮助: 这是我完成的代码,它可以工作。将鼠标放在画布上的任意位置按住鼠标并拖动以创建矩形。它可以使用更多改进来在任何方向拖动和创建矩形。

      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;
      }
      

      【讨论】:

      • 您好用户,很高兴您找到了解决方案。看到我的回答很有帮助,你的解决方案就是以此为基础的,所以请将我的回答标记为本帖的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2012-12-08
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      相关资源
      最近更新 更多