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());
        }
    }
}

运行结果:

1bab40817e571609d8d733230501c8c2.png

欢迎关注公众号: dotnet编程大全

技术群: 需要进技术群的添加小编微信mm1552923,备注:加群;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WPF的Viewport3D显示图片,需要使用Material对象将图片添加到3D模型的表面。以下是一些可能导致图片在Viewport3D显示的常见问题和解决方法: 1. 检查文件路径:首先确保图片文件的路径是正确的,并且图像文件位于你的项目访问的位置。可以尝试使用绝对路径或者使用相对路径来解决此问题。 ``` <ImageBrush ImageSource="C:\FilePath\ImageFile.jpg" /> ``` 2. 检查图片尺寸:确保所使用的图片尺寸不会超过WPF视口的可见区域。如果图像尺寸超出视口的大小,可能会导致图像无法显示或者部分显示。可以尝试调整图像的尺寸来解决该问题。 3. 检查相机设置:确保相机位于正确的位置并且指向正确的方向。可以尝试调整相机的位置和方向,使其能够捕捉到图像。 4. 检查光照设置:确保适当的光照设置,以便图像能够正确地显示。如果光照设置不正确,可能会导致图像看起来暗淡或者无法显示。可以尝试调整光源的位置和强度来解决该问题。 5. 检查模型设置:确保正确地将图像添加到3D模型的表面。可以尝试使用合适的Material对象将图像添加到模型的表面,例如使用DiffuseMaterial或者ImageBrush。 如果按照上述步骤进行检查和调整仍然无法显示图像,则可能需要进行更详细的调试和分析,查看是否存在其他因素导致图像无法正确显示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值