使用矩形在图像上创建搜索区域

本文关键字:创建 搜索 区域 图像 | 更新日期: 2023-09-27 18:19:31

我有一个显示图像的图像查看器。我想用鼠标在图像上画一个矩形,并获得矩形的x和y坐标(X1、X2、Y1和Y2)。我将使用这些坐标创建一个搜索区域,并在一个数组中找到最大值和最小值,该数组在两个轴上的像素数与图像的像素数完全相同
有人能给我指个方向开始吗?

使用矩形在图像上创建搜索区域

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

示例:

主窗口.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>

主窗口.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表达式将矩形重新定位限制在画布区域,检查画布区域是否包含上的鼠标位置

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

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