矩形画框控件代码

这段代码展示了一个WPF应用程序,它利用OpenCvSharp库加载和显示图片。用户可以在图像上绘制矩形,通过鼠标事件(_canvas_MouseLeftButtonDown,_canvas_MouseMove,_canvas_MouseLeftButtonUp)来控制矩形的绘制。程序计算并设置缩放比例以适应图像显示,并提供了属性和方法来更新和获取当前的BitmapSource。
摘要由CSDN通过智能技术生成

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>

如果有需要把控件的点传出来,就加一个委托就行,懒得写了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值