用途

通过使用C++ + Opencv 编写算法,然后用WPF(C#)编写程序界面,实现交互

可以参考MSDN文档:https://docs.microsoft.com/en-us/cpp/windows/pin-ptr-cpp-cli?view=vs-2017

项目结构

代码

CLR部分:

CLRLibrary.h文件

#pragma once

using namespace System;
using namespace System::Collections::Generic;
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
namespace CLRLibrary { public ref class Class1
{
public:
List<Byte>^ ErodeImage(System::String^ filename);
int Rows;
int Cols; };
}

CLRLibrary.cpp文件

#include "stdafx.h"
using namespace System;
using namespace System::Runtime::InteropServices;
#include "CLRLibrary.h"
#include "H:\CPP\WPF_CPPCLI_Demo\ConsoleLibrary\ErodeClass.h"
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
Mat ErodeAction(const char* filename)
{
Mat img = imread(filename);
Mat out;
//获取自定义核
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); //第一个参数MORPH_RECT表示矩形的卷积核,当然还可以选择椭圆形的、交叉型的
//膨胀操作
dilate(img, out, element); return out;
}
//Erode The Image Func
List<Byte>^ CLRLibrary::Class1::ErodeImage(System::String ^ filename)
{
//将String^转换成const char*
IntPtr ip = Marshal::StringToHGlobalAnsi(filename);
const char* str = static_cast<const char*>(ip.ToPointer());
Mat result=ErodeAction(str);
List<Byte>^ ret=gcnew List<Byte>();
for (int i = 0; i < result.rows; i++)
{
for (int j = 0; j < result.cols; j++)
{
//RBG
ret->Add(result.at<Vec3b>(i, j)[0]);
ret->Add(result.at<Vec3b>(i, j)[1]);
ret->Add(result.at<Vec3b>(i, j)[2]);
}
}
Rows = result.rows;
Cols = result.cols; return ret;
}

WPF部分:

MainWindow.xaml:

<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Loaded="MainWindow_OnLoaded">
<Grid>
<Image Name="Box"></Image>
</Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
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 CLRLibrary;
using Color = System.Drawing.Color; namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} /// <summary>
/// 将BitMap转换成ImageSource
/// </summary>
/// <param name="bitmap"></param>
/// <returns></returns>
public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{
//Bitmap bitmap = icon.ToBitmap();  
IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()); return wpfBitmap;
} private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var drawClass=new Class1(); List<byte> RGBData=drawClass.ErodeImage("D:/Background/1.jpg"); Bitmap map = new Bitmap(drawClass.Cols, drawClass.Rows);
int temp = 0;
for (int i = 0; i < drawClass.Rows; i++)
{
for (int j = 0; j < drawClass.Cols; j++)
{
var color = Color.FromArgb(RGBData[temp], RGBData[temp + 1], RGBData[temp + 2]);
temp += 3;
map.SetPixel(j, i, color);
}
}
Box.Source = ChangeBitmapToImageSource(map);
} }
}

运行效果图

项目所有代码全在上面,如果有需要源码的或者需要交流的可以加我QQ或者微信。

关于更多的C++/CLI代码可以参考我上面给的MSDN文档链接,基本用得到的语法都能从上面找到。

[WPF]C#调用C++代码(通过C++/CLI)的更多相关文章

  1. WPF 判断调用方法堆栈

    原文:WPF 判断调用方法堆栈 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee.io 访问博客 ...

  2. C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式

    C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...

  3. 如何实现 javascript “同步”调用 app 代码

    在 App 混合开发中,app 层向 js 层提供接口有两种方式,一种是同步接口,一种一异步接口(不清楚什么是同步的请看这里的讨论).为了保证 web 流畅,大部分时候,我们应该使用异步接口,但是某些 ...

  4. 1.JAVA中使用JNI调用C++代码学习笔记

    Java 之JNI编程1.什么是JNI? JNI:(Java Natibe Inetrface)缩写. 2.为什么要学习JNI?  Java 是跨平台的语言,但是在有些时候仍然是有需要调用本地代码 ( ...

  5. 股票数据调用示例代码php

    <!--?php // +---------------------------------------------------------------------- // | JuhePHP ...

  6. 在HTML标签<a/>中调用javascript代码

    <a/>标签的“href”属性可以是一个有效的URL,表示跳转的目的地,除此之外,href还可以是一段javascript代码.当为“href”设置javascript代码时,格式如下:& ...

  7. 在Java中直接调用js代码(转载)

    http://blog.csdn.net/xzyxuanyuan/article/details/8062887 JDK1.6版添加了新的ScriptEngine类,允许用户直接执行js代码. 在Ja ...

  8. java调用python代码

    同样的我们需要安装jython,具体的步骤如下: 1. 去 http://sourceforge.net/projects/jython/ 下载最新的jython相关的jar包. 2. 下载下来的ja ...

  9. Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示

    如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...

  10. 在WPF中调用Winform控件

    最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...

随机推荐

  1. Vue学习之--------Vue生命周期beforeCreate、created、beforeMount、mounted、beforeDestroy 。。。(图解详细过程)(2022/7/17)

    文章目录 1.Vue生命周期 1.1 概念 1.2 图解 2.钩子函数的用法说明 2.1 beforeCreate()和created()的详细讲述 2.1.1 方法说明 2.1.2 代码实例 2.1 ...

  2. Linux执行jsp命令的时候报错:-bash: jps: command not found

    前言:在zookeeper学习的时候,执行jsp命令查看zookpper运行状态的时候发现报错: -bash: jps: command not found 翻阅了一大批文章,不是东拼西凑,就是缺斤少 ...

  3. python face_recognition安装及各种应用

    1.安装 首先,必须提前安装cmake.numpy.dlib,其中,由于博主所用的python版本是3.6.4(为了防止不兼容,所以用之前的版本),只能安装19.7.0及之前版本的dlib,所以直接p ...

  4. 重新整理 .net core 实践篇 ———— linux 上线篇 [外篇]

    前言 简单整理一个linux 简单上线. 这个是该系列的外篇,该系列继续更新.献给刚学的人. 正文 安装实例 dotnet new webapp -n AspNetCoreDemo -o firstw ...

  5. numpy常用知识点备忘

    常用函数 a.max(axis=0) a.max(axis=1) a.argmax(axis=1) : 每列的最大值(在行方向找最大值).每行的最大值(在列方向找对大致).最大值的坐标 sum()求和 ...

  6. adb 安装与使用

    什么是adb adb 是有个通用命令行工具 他允许您与模拟器实例或者链接的Android设备进行通信,他可为各种设备操作提供便利,比如安装和调试应用 启动adb 服务在命令行中输入adb start- ...

  7. clip-path属性深入理解与使用

    clip-path CSS 属性可以创建一个只有元素的部分区域可以显示的剪切区域.区域内的部分显示,区域外的隐藏. clip-path的属性值可以是以下几种: 1.inset: 将元素剪裁为一个矩形, ...

  8. Ubuntu定时执行python脚本

    Crontab命令 命令选项: crontab -l : 显示某个用户的任务计划 crontab -e :编辑某个用户的任务计划 cron服务 service cron start/stop/rest ...

  9. 这是不是你想要了解SQL的艺术,基础语法等等

    一.基础sql语句: 模块定义 基础语句 基础功能 数据定义 create table 创建数据库表 drop table 删除数据表 alter table 修改表结构 create view 创建 ...

  10. 【题解】UVA10228 A Star not a Tree?

    题面传送门 解决思路 本题数据范围较小,可以使用模拟退火算法(随机化). 顾名思义,模拟退火就是一个类似于降温的过程.先设置一个较大的初温,每次随机改变状态,若使答案更优,则采取更优答案,否则根据其与 ...