使用c#制作一个小型桌面程序

server/2024/9/23 16:59:46/

封装dll

首先使用visual stdio 创建Dll新项目,然后属性管理器导入自己的工程属性表(如果没有可以参考visual stdio 如何配置opencv等其他环境)

创建完成后 系统会自动生成一些文件,其中 pch.cpp 先不要修改,pch.h中先导入自己需要用到的库,下面是我的代码

pch.h

#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
#include <string>

现在编写我们的接口代码,我封装的是resnet18的代码:
首先添加源文件ResNetDll.cpp:

ResNetDll.cpp

#include "pch.h"
#include "ResNetDll.h"// 全局变量,用于存储模型路径和图像路径
static std::string g_imagePath;
static std::string g_modelPath;// 图像预处理函数
cv::Mat transformImage(const std::string& imagePath) {cv::Mat image = cv::imread(imagePath);if (image.empty()) {throw std::runtime_error("Failed to load image.");}cv::Mat resizedImage;cv::resize(image, resizedImage, cv::Size(224, 224));cv::Mat floatImage;resizedImage.convertTo(floatImage, CV_32F, 1.0 / 255.0);cv::Mat normalizedImage;cv::Scalar mean(0.485, 0.456, 0.406);cv::Scalar stdDev(0.229, 0.224, 0.225);cv::subtract(floatImage, mean, normalizedImage);cv::divide(normalizedImage, stdDev, normalizedImage);// 从 BGR 转换到 RGBcv::Mat rgbImage;cv::cvtColor(normalizedImage, rgbImage, cv::COLOR_BGR2RGB);return rgbImage;
}// 推理函数
const char* run_inference() {static std::string result;try {// 加载 ONNX 模型cv::dnn::Net net = cv::dnn::readNetFromONNX(g_modelPath);if (net.empty()) {result = "Failed to load the model.";return result.c_str();}// 预处理图像cv::Mat rgbImage = transformImage(g_imagePath);// 创建 blob 并设置为网络输入cv::Mat blob = cv::dnn::blobFromImage(rgbImage, 1.0, cv::Size(224, 224), cv::Scalar(), true, false);net.setInput(blob);// 执行推理cv::Mat output = net.forward();// 处理输出cv::Mat prob = output.reshape(1, 1);  // 变换成 1D 张量cv::Point classIdPoint;double confidence;// 用来找到矩阵或图像中元素的最小值和最大值,以及它们所在的位置cv::minMaxLoc(prob, 0, &confidence, 0, &classIdPoint);int classId = classIdPoint.x;// 根据预测结果返回相应的标签result = "Predicted Class ID: " + std::to_string(classId) + " with confidence: " + std::to_string(confidence);return result.c_str();}catch (const std::exception& e) {result = "Error occurred during inference: " + std::string(e.what());return result.c_str();}
}// DLL 暴露的函数,用于设置图像路径
extern "C" RESNETDLL_API void set_image_path(const char* imagePath) {g_imagePath = imagePath;
}// DLL 暴露的函数,用于设置模型路径
extern "C" RESNETDLL_API void set_model_path(const char* modelPath) {g_modelPath = modelPath;
}// DLL 暴露的函数,运行推理
extern "C" RESNETDLL_API const char* run_resnet() {return run_inference();
}

ResNetDll.h:

#pragma once#ifdef RESNETDLL_EXPORTS
#define RESNETDLL_API __declspec(dllexport)
#else
#define RESNETDLL_API __declspec(dllimport)
#endifextern "C" {// 设置图像路径RESNETDLL_API void set_image_path(const char* imagePath);// 设置模型路径RESNETDLL_API void set_model_path(const char* modelPath);// 运行推理RESNETDLL_API const char* run_resnet();
}

点击生成dll,就封装成了windows动态库

制作Demo

创建.NET Framework新项目,将之前生成的dll放在Demo文件夹的bin ->debug或是 release中(看你自己用的什么模式),
新建NativeMethods.cs 这个文件用于 导入 dll中的接口函数或类
我的代码如下

NativeMethods.cs

using System;
using System.Runtime.InteropServices;namespace ResNetApp
{public static class NativeMethods{// 导入 DLL 中的 set_image_path 函数[DllImport("ResNetDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void set_image_path(string imagePath);// 导入 DLL 中的 set_model_path 函数[DllImport("ResNetDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void set_model_path(string modelPath);// 导入 DLL 中的 run_resnet 函数[DllImport("ResNetDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern IntPtr run_resnet();}
}

然后在窗口中拉入你想要的控件,这是我的窗口布局
在这里插入图片描述
布局完了之后会自动生成Form1.Designer.cs 的窗口设计代码,点击控件按F4 还可以修改他们的属性

Form1.cs

这个代码 编写你想要每个控件实现的功能:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace ResNetApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void buttonSelectImage_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = "图像文件|*.bmp;*.jpg;*.jpeg;*.png";if (openFileDialog.ShowDialog() == DialogResult.OK){textBoxImagePath.Text = openFileDialog.FileName; // 显示选择的图像路径}}private void buttonSelectModel_Click(object sender, EventArgs e){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.Filter = "ONNX 模型文件|*.onnx";if (openFileDialog.ShowDialog() == DialogResult.OK){textBoxModelPath.Text = openFileDialog.FileName; // 显示选择的模型路径}}private void button1_Click(object sender, EventArgs e){try{string imagePath = textBoxImagePath.Text;string modelPath = textBoxModelPath.Text;if (string.IsNullOrEmpty(imagePath) || string.IsNullOrEmpty(modelPath)){textBox1.Text = "请选择图像和模型路径。";return;}textBox1.Text = "开始运行 ResNet ...";// 设置图像路径和模型路径NativeMethods.set_image_path(imagePath);NativeMethods.set_model_path(modelPath);// 调用 DLL 执行推理IntPtr resultPtr = NativeMethods.run_resnet();// 将返回的指针转换为字符串string result = Marshal.PtrToStringAnsi(resultPtr);// 显示结果textBox1.Text = result;}catch (Exception ex){textBox1.Text = "错误: " + ex.Message;}}}
}

Program.cs

我们还需要一个入口主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;namespace ResNetApp
{static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}
}

完成之后点击生成 就可以在bin中出现的你的.exe文件咯,是不是很简单呀~[狗头]


http://www.ppmy.cn/server/120883.html

相关文章

解决phpstudy无法启动MySQL服务

三种方法 如果说你在小皮里面&#xff0c;启动mysql&#xff0c;发现启动不了&#xff0c;而且你在你自己电脑本地有装过mysql服务&#xff0c;那么可以按照我下面的不走来&#xff0c;按顺序试验1&#xff0c;2&#xff0c;3,三个里面肯定有一个是可以解决的 1.停止本地的mysq…

Python知识点:如何使用Python进行算法交易

开篇&#xff0c;先说一个好消息&#xff0c;截止到2025年1月1日前&#xff0c;翻到文末找到我&#xff0c;赠送定制版的开题报告和任务书&#xff0c;先到先得&#xff01;过期不候&#xff01; 使用Python进行算法交易的完整指南 在当今快节奏的金融市场中&#xff0c;算法…

ADB ROOT开启流程

开启adb root 选项后&#xff0c;执行如下代码&#xff1a; packages/apps/Settings/src/com/android/settings/development/AdbRootPreferenceController.java mADBRootService new ADBRootService(); Override public boolean onPreferenceChange(Preference preference…

Git提交类型

说明&#xff1a;Git提交类型指的是代码commit时&#xff0c;写在comment前面的标志&#xff0c;表示此次commit的提交类型&#xff0c;如下&#xff1a; Git提交类型 常见的Git提交类型有&#xff1a; feat&#xff1a;新特性、新功能或优化&#xff1b; fix&#xff1a;修复…

react + antDesignPro 企业微信扫码登录

效果 实现步骤 1、项目中document.ejs文件引入企微js链接 注意&#xff1a;技术栈是使用的react antDesignPro&#xff0c;不同的技术栈有不同的入口文件&#xff08;如vue在html文件引入&#xff09; <script src"https://wwcdn.weixin.qq.com/node/wework/wwopen/j…

代码随想录算法day38 | 动态规划算法part11 | 1143.最长公共子序列,1035.不相交的线,53. 最大子序和,392.判断子序列

1143.最长公共子序列 体会一下本题和 718. 最长重复子数组 的区别 力扣题目链接(opens new window) 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长公共子序列的长度。 一个字符串的 子序列 是指这样一个新的字符串&#xff1a;它是由原字符串在不改变字符的…

【Webpack--006】处理字体图标资源

&#x1f913;&#x1f60d;Sam9029的CSDN博客主页:Sam9029的博客_CSDN博客-前端领域博主 &#x1f431;‍&#x1f409;若此文你认为写的不错&#xff0c;不要吝啬你的赞扬&#xff0c;求收藏&#xff0c;求评论&#xff0c;求一个大大的赞&#xff01;&#x1f44d;* &#x…

外包干了4年,技术退步太明显了。。。。。

先说一下自己的情况&#xff0c;本科生生&#xff0c;20年通过校招进入武汉某软件公司&#xff0c;干了差不多4年的功能测试&#xff0c;今年国庆&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了四年的功能…