前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF中图片剪裁并显示

WPF中图片剪裁并显示

作者头像
zls365
发布2020-12-29 12:05:50
1K0
发布2020-12-29 12:05:50
举报
文章被收录于专栏:CSharp编程大全CSharp编程大全

XAML:

代码语言:javascript
复制
<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
代码语言:javascript
复制
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());
        }
    }
}

运行结果:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档