[WPF]C#调用C++代码(通过C++/CLI)
用途#
通过使用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文档链接,基本用得到的语法都能从上面找到。
作者:lizhenghao126
出处:https://www.cnblogs.com/lizhenghao126/p/11053558.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
https://github.com/li-zheng-hao
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示