wpf中的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using OpenCvSharp.Extensions;
using System.Drawing;
namespace rectangleTool
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window, INotifyPropertyChanged
{
private BitmapSource currentbitmapSource;
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public BitmapSource CurrentBitmapSource { get { return currentbitmapSource; } set { currentbitmapSource = value; OnPropertyChanged(); } }
private double scale;
public MainWindow()
{
InitializeComponent();
Mat im = Cv2.ImRead(@"D:\rectangleTool\rectangleTool\1.jpg");
SetImage(im);
}
public void SetImage(Mat src)
{
Mat src2 = src;
base.Dispatcher.Invoke(() => {
SetImage(src.Clone().ToBitmapSource());
}
);
}
public void SetImage(BitmapSource bitmapSource)
{
BitmapSource bitmapSource2 = bitmapSource;
cameraImage.Dispatcher.Invoke(() =>
{
CurrentBitmapSource = bitmapSource2;
cameraImage.Source = bitmapSource2;
double num = _canvas.Width / bitmapSource2.Width;
double num2 = _canvas.Height / bitmapSource2.Height;
scale = Math.Min(num, num2);
_Scale.ScaleY = scale;
_Scale.ScaleY = scale;
});
}
public System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle();
System.Windows.Point mousetemp = new System.Windows.Point();
public bool isCandrawingRect = true;
private void _canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Canvas canvas = sender as Canvas;
canvas.CaptureMouse();
if (canvas == null)
{
return;
}
System.Windows.Point mouseXY = e.GetPosition(canvas);
if (isCandrawingRect)
{
Canvas.SetLeft(rectangle, mouseXY.X);
Canvas.SetTop(rectangle, mouseXY.Y);
rectangle.Width = 0;
rectangle.Height = 0;
rectangle.Stroke = new SolidColorBrush(Colors.Red);
mousetemp.X = mouseXY.X;
mousetemp.Y = mouseXY.Y;
_canvas.Children.Add(rectangle);
}
}
private void _canvas_MouseMove(object sender, MouseEventArgs e)
{
Canvas canvas = (Canvas)sender;
System.Windows.Point CurmousePoint = new System.Windows.Point();
CurmousePoint = e.GetPosition(canvas);
if (isCandrawingRect)
{
rectangle.Width = Math.Abs(CurmousePoint.X - mousetemp.X);
rectangle.Height = Math.Abs(CurmousePoint.Y - mousetemp.Y);
Canvas.SetLeft(rectangle,Math.Min(mousetemp.X, CurmousePoint.X));
Canvas.SetTop(rectangle, Math.Min(mousetemp.Y, CurmousePoint.Y));
}
}
private void _canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Canvas canvas = (Canvas)sender;
System.Windows.Point CurmousePoint = new System.Windows.Point();
CurmousePoint = e.GetPosition(canvas);
if (isCandrawingRect) {
rectangle.Width = Math.Abs(CurmousePoint.X - mousetemp.X);
rectangle.Height = Math.Abs(CurmousePoint.Y - mousetemp.Y);
Canvas.SetLeft(rectangle, Math.Min(mousetemp.X, CurmousePoint.X));
Canvas.SetTop(rectangle, Math.Min(mousetemp.Y, CurmousePoint.Y));
canvas.ReleaseMouseCapture();
isCandrawingRect = false;
}
}
}
}
xaml
<Window x:Class="rectangleTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:rectangleTool"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<Canvas x:Name="_canvas" Width="800"
Height="600"
HorizontalAlignment="Left"
VerticalAlignment="Top" MouseLeftButtonDown="_canvas_MouseLeftButtonDown" MouseMove="_canvas_MouseMove" MouseLeftButtonUp="_canvas_MouseLeftButtonUp">
<Image x:Name="cameraImage" Stretch="Fill"
Source="{Binding CurrentBitmapSource,RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor}}">
<Image.RenderTransform>
<ScaleTransform x:Name="_Scale" ></ScaleTransform>
</Image.RenderTransform>
</Image>
</Canvas>
</Grid>
</Window>
如果有需要把控件的点传出来,就加一个委托就行,懒得写了