WPF的Grid面板加载图片鼠标画框

最近开始做WPF界面使用Grid显示图像,通过鼠标画框对部分图像进行选择

xaml代码







cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace GridImageSelect
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        Line oneTotwo;
        Line oneTothree;
        Line twoTofour;
        Line threeTofour;

        int clicktimes = 0;


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            grid1.Children.Clear();
            clicktimes = 0;
            label1.Content = "点击位置: ";
            label2.Content = "离开位置: ";
        }

        private void grid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int x = Convert.ToInt32(e.GetPosition(grid1).X);
            int y = Convert.ToInt32(e.GetPosition(grid1).Y);
            label1.Content = "点击位置: " + Convert.ToString(x) + "," + Convert.ToString(y);
            if (0 == clicktimes)
            {
                Point clickPoint = (Point)e.GetPosition(grid1);
                #region
                oneTotwo = new Line();
                oneTotwo.Stroke = Brushes.Red;
                oneTotwo.Fill = Brushes.Red;
                oneTotwo.StrokeLineJoin = PenLineJoin.Bevel;
                oneTotwo.X1 = clickPoint.X;
                oneTotwo.Y1 = clickPoint.Y;
                oneTotwo.X2 = clickPoint.X + 10;
                oneTotwo.Y2 = clickPoint.Y;
                oneTotwo.StrokeThickness = 1;
                grid1.Children.Add(oneTotwo);
                int zindex = grid1.Children.Count;
                Grid.SetZIndex(oneTotwo, zindex);

                oneTothree = new Line();
                oneTothree.Stroke = Brushes.Red;
                oneTothree.Fill = Brushes.Red;
                oneTothree.StrokeLineJoin = PenLineJoin.Bevel;
                oneTothree.X1 = clickPoint.X;
                oneTothree.Y1 = clickPoint.Y;
                oneTothree.X2 = clickPoint.X;
                oneTothree.Y2 = clickPoint.Y + 10;
                oneTothree.StrokeThickness = 1;
                grid1.Children.Add(oneTothree);
                zindex = grid1.Children.Count;
                Grid.SetZIndex(oneTothree, zindex);

                twoTofour = new Line();
                twoTofour.Stroke = Brushes.Red;
                twoTofour.Fill = Brushes.Red;
                twoTofour.StrokeLineJoin = PenLineJoin.Bevel;
                twoTofour.X1 = clickPoint.X + 10;
                twoTofour.Y1 = clickPoint.Y;
                twoTofour.X2 = clickPoint.X + 10;
                twoTofour.Y2 = clickPoint.Y + 10;
                twoTofour.StrokeThickness = 1;
                grid1.Children.Add(twoTofour);
                zindex = grid1.Children.Count;
                Grid.SetZIndex(twoTofour, zindex);

                threeTofour = new Line();
                threeTofour.Stroke = Brushes.Red;
                threeTofour.Fill = Brushes.Red;
                threeTofour.StrokeLineJoin = PenLineJoin.Bevel;
                threeTofour.X1 = clickPoint.X;
                threeTofour.Y1 = clickPoint.Y + 10;
                threeTofour.X2 = clickPoint.X + 10;
                threeTofour.Y2 = clickPoint.Y + 10;
                threeTofour.StrokeThickness = 1;
                grid1.Children.Add(threeTofour);
                zindex = grid1.Children.Count;
                Grid.SetZIndex(threeTofour, zindex);
                clicktimes++;
            }
                #endregion
 
        }

        private void grid1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            int x = Convert.ToInt32(e.GetPosition(grid1).X);
            int y = Convert.ToInt32(e.GetPosition(grid1).Y);
            label2.Content = "离开位置: " + Convert.ToString(x) + "," + Convert.ToString(y);
            oneTotwo = null;
            oneTothree = null;
            twoTofour = null;
            threeTofour = null;
        }

        private void grid1_MouseMove(object sender, MouseEventArgs e)
        {
            Point drawPoint = (Point)e.GetPosition(grid1);
            if (oneTotwo != null & twoTofour != null & oneTothree != null & threeTofour != null & e.LeftButton == MouseButtonState.Pressed)
            {
                twoTofour.X1 = oneTotwo.X2 = twoTofour.X2 = threeTofour.X2 = drawPoint.X;
                threeTofour.Y1 = threeTofour.Y2 = twoTofour.Y2 = twoTofour.Y2 = oneTothree.Y2 = drawPoint.Y;
            }
        }

        private void grid1_Loaded(object sender, RoutedEventArgs e)
        {
            
            BitmapImage img = new BitmapImage(new Uri(@"image/1.jpg", UriKind.Relative));
            ImageBrush ImageOne = new ImageBrush();
            ImageOne.ImageSource = img;
            ImageOne.Stretch = Stretch.Fill;
            grid1.Background = ImageOne;//显示图片
        }
    }
}
运行效果图


你可能感兴趣的:(WPF的Grid面板加载图片鼠标画框)