WPF中图片剪裁并显示

XAML:

<Window x:Class="WpfApp6.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:WpfApp6"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="400" Loaded="Window_Loaded">
    <Grid ShowGridLines="True" >
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Grid.Row="0" Grid.Column="0" Name="image1" Source="C:\Users\admin\source\repos\WpfApp6\WpfApp6\bin\Debug\11.jpg"/>
        <Image Grid.Row="0" Grid.Column="1" Name="image2" />
        <Image Grid.Row="1" Grid.Column="0" Name="image3" />
        <Image Grid.Row="1" Grid.Column="1" Name="image4" />
    </Grid>
</Window>


wpf代码:

  1. 首先要在nuget上安装emgucv

using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System;
using System.Windows;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace WpfApp6
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);


        public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());


            if (!DeleteObject(hBitmap))
            {
                throw new System.ComponentModel.Win32Exception();
            }
            return wpfBitmap;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


            Rectangle rectangle = new Rectangle(100, 100, 212, 564);//int x, int y, int width, int height
           
            /***加载image类型图片剪裁显示到wpf image控件 ***/
            Image<Bgr, byte> Img = new Image<Bgr, byte>(new Bitmap(@"C:\Users\admin\source\repos\WpfApp6\WpfApp6\bin\Debug\11.jpg"));//路径声明
            Image<Bgr, byte> Sub1 = Img.GetSubRect(rectangle);
            image2.Source = ChangeBitmapToImageSource(Sub1.ToBitmap());


            /***加载mat类型图片剪裁显示到wpf image控件***/
            Mat SCr = new Mat(@"C:\Users\admin\source\repos\WpfApp6\WpfApp6\bin\Debug\22.jpg", LoadImageType.AnyColor);
            image3.Source = ChangeBitmapToImageSource(SCr.Bitmap);
            Image<Bgr, byte> Sub2 = SCr.ToImage<Bgr, byte>().GetSubRect(rectangle);
            image4.Source = ChangeBitmapToImageSource(Sub2.ToBitmap());
        }
    }
}


运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值