news 2026/3/11 19:37:23

联合编程(加载单个工具,ini读写,图片读写,setting存储)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
联合编程(加载单个工具,ini读写,图片读写,setting存储)

加载单个工具例子

//保存到一个地方 让vs进行读取

namespace 加载单个pma工具
{
public partial class Form1 : Form
{
CogPMAlignTool pma;
public Form1()
{
InitializeComponent();
cogRecordDisplay1.Fit();
}
//窗体加载事件
private void Form1_Load(object sender, EventArgs e)
{
//加载vpp资源(重点)
pma = CogSerializer.LoadObjectFromFile("
模版匹配.vpp") as CogPMAlignTool;//位置
//Subject 属性 工具展示的内容
cogPMAlignEditV21.Subject = pma;
//MessageBox.Show("加载成功");

}

private void button1_Click(object sender, EventArgs e)
{
if (pma==null)
{
MessageBox.Show("加载失败");
return;
}
OpenFileDialog dia = new OpenFileDialog();//文件对话框
dia.Filter = "All Image Files|*.jpg;*.png;*.jpeg;*.ico;*.tif;*.tiff;*.idb;*.bmp\"";
dia.Title = "请选择文件";
if (dia.ShowDialog() == DialogResult.OK)
{
Bitmap map = Image.FromFile(dia.FileName) as Bitmap;//加载图片路径
pma.InputImage = new CogImage8Grey(map);//转成8位灰图赋值给pma


}
}

private void button2_Click(object sender, EventArgs e)
{
if (pma ==null)
{
MessageBox.Show("加载资源失败");
return;
}
pma.Run();
//record 展示一条记录
//CreateLastRunRecord 创建最近的运行记录
//SubRecords 子记录
cogRecordDisplay1.Record = pma.CreateLastRunRecord().SubRecords[0];
cogRecordDisplay1.Fit();

}
}
}

ini格式读写

namespace ini文件存储读取
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// FileStream a = new FileStream(@"1", FileMode.Open, FileAccess.Write);
//StreamWriter a2 = new StreamWriter(a, Encoding.UTF8);
}


//写入


private void button2_Click(object sender, EventArgs e)
{
//1 写入指定节点指定的键 (没用则创建一个新键)
//IniAPI.INIWriteValue("C:\\Users\\12345\\OneDrive\\Desktop\\1.ini","net","ip","333");
//2 删除节点
//IniAPI.INIDeleteSection("C:\\Users\\12345\\OneDrive\\Desktop\\1.ini","data");
//3 删除键
//IniAPI.INIDeleteKey("C:\\Users\\12345\\OneDrive\\Desktop\\1.ini","net","ip");

}


//读取


private void button1_Click(object sender, EventArgs e)
{
// //1 获取所有的节点名称
//string[] s1 = IniAPI.INIGetAllSectionNames("C:\\Users\\18060\\OneDrive\\Desktop\\1.ini");
// foreach (string item in s1)
// {
// richTextBox1.Text += item +"\n";
// }

// 2 获取所有的items
//string[] s1 = IniAPI.INIGetAllItems("C:\\Users\\18060\\OneDrive\\Desktop\\1.ini","net");
//foreach (string item in s1)
//{
// richTextBox1.Text += item + "\n";
//}
// 3 获取对应节点的键的值(重要)
//string s1 = IniAPI.INIGetStringValue("C:\\Users\\18060\\OneDrive\\Desktop\\1.ini","net","ip","");
//richTextBox1.Text = s1;


}
}
}

图片读写操作

namespace 图片读写操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

方法一

private void button1_Click(object sender, EventArgs e)
{
//1 创建CogImageFileTool工具
CogImageFileTool fileTool = new CogImageFileTool();
OpenFileDialog dia = new OpenFileDialog();//文件对话框
dia.Filter = "All Image Files|*.jpg;*.png;*.jpeg;*.ico;*.tif;*.tiff;*.idb;*.bmp\"";
dia.Title = "请选择文件";
if (dia.ShowDialog() == DialogResult.OK)
{
//可以获取选择文件的路径
//fileTool.Operator.Open 打开fileTool工具
//参数1 路径
//参数2 读取fileTool工具
// 2 使用fileTool工具读取图片
fileTool.Operator.Open(dia.FileName,CogImageFileModeConstants.Read);
// 3 运行工具
fileTool.Run();
// 4 获取输出图 展示
cogRecordDisplay1.Image = fileTool.OutputImage;
// 5 自适应大小
cogRecordDisplay1.Fit();

}
}

private void button2_Click(object sender, EventArgs e)
{
CogImageFileTool fileTool = new CogImageFileTool();
//保存控件展示的图片
//把控件图片赋值给工具的输入图参数中
fileTool.InputImage = cogRecordDisplay1.Image;
//fileTool.Operator.Open 打开fileTool工具
//参数1 路径
//参数2 写入fileTool工具
// 2 使用fileTool工具写入图片
fileTool.Operator.Open(@"1.png", CogImageFileModeConstants.Write);
// 3 运行工具
fileTool.Run();


}

方法二


//使用位图进行读取展示


private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog dia = new OpenFileDialog();//文件对话框
dia.Filter = "All Image Files|*.jpg;*.png;*.jpeg;*.ico;*.tif;*.tiff;*.idb;*.bmp\"";
dia.Title = "请选择文件";
if (dia.ShowDialog() == DialogResult.OK)
{
Bitmap bit = Image.FromFile(dia.FileName) as Bitmap;

CogImage8Grey bit1 = new CogImage8Grey(bit);//转化8位灰图
CogImage24PlanarColor bit2 =new CogImage24PlanarColor(bit);//转化为24位彩色图
cogRecordDisplay1.Image = bit2; //展示图片
cogRecordDisplay1.Fit();

}
}
//使用位图进行写入保存
private void button4_Click(object sender, EventArgs e)
{
string path = Directory.GetCurrentDirectory()+"\\data";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string path2 = path + "\\1.png";
//if (!File.Exists(path2))
//{
// return;
//}

//CreateContentBitmap创建位图内容
//参数1 把图片转成位图
Bitmap map = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Image)as Bitmap;
map.Save(path+$"\\{DateTime.Now:yyyyMMddhhmmss}.png");

}
}

setting存储

private void button1_Click(object sender, EventArgs e)
{
Settings set = Settings.Default;//获取配置对象
set.name = textBox1.Text;
set.age =int.Parse(textBox2.Text);
set.weight = float.Parse(textBox3.Text);
set.sex = bool.Parse(textBox4.Text);
set.Save();//保存更改

}
//读取数据
private void button2_Click(object sender, EventArgs e)
{
Settings settings = Settings.Default;
string name = settings.name;//获取配置表数据
int age = settings.age;
float weight = settings.weight;
bool sex = settings.sex;
richTextBox1.Text = name+"\n"+age+"\n"+weight+"\n"+sex;

}

//持久化存储
//json格式 {"age":10}
//file文件存储
//二进制存储方式
//csv存储格式 姓名,年龄
// 张三,10
//setting存储格式
//ini存储格式 [net]
// IP = 127.0.0.1
// port = 8080
//xml存储
//数据库

//存储数据

//内存存储: 数据类型存储方式 数组,字典,集合

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/9 20:18:18

SpringBoot+Vue +周边游平台平台完整项目源码+SQL脚本+接口文档【Java Web毕设】

摘要 随着旅游业的发展和人们生活水平的提高,周边游逐渐成为大众休闲娱乐的重要选择。传统的旅游服务模式存在信息不对称、预订流程繁琐、用户体验不佳等问题,难以满足现代游客的个性化需求。互联网技术的快速发展为旅游行业提供了新的解决方案&#xf…

作者头像 李华
网站建设 2026/3/8 15:23:49

基于SpringBoot+Vue的政府管理系统管理系统设计与实现【Java+MySQL+MyBatis完整源码】

摘要 随着信息技术的快速发展,数字化政务管理已成为提升政府工作效率和服务质量的重要手段。传统政府管理系统中存在信息孤岛、数据冗余、响应速度慢等问题,亟需通过信息化手段实现业务流程的优化和数据的集中管理。政府管理系统通过整合各部门资源&…

作者头像 李华
网站建设 2026/3/9 22:17:27

Emby高级功能解锁与媒体服务器配置全指南

Emby高级功能解锁与媒体服务器配置全指南 【免费下载链接】emby-unlocked Emby with the premium Emby Premiere features unlocked. 项目地址: https://gitcode.com/gh_mirrors/em/emby-unlocked 想要低成本体验Emby媒体服务器的高级功能?通过科学配置实现E…

作者头像 李华
网站建设 2026/3/11 9:12:06

为什么前后端分离了,我们比从前更痛苦?

引言:技术进步的悖论前后端分离是近年来Web开发领域最重要的架构变革之一,它代表着专业化分工的进步,理论上应提升开发效率和应用质量。然而在实践中,许多团队却发现自己陷入了新的困境:沟通成本指数级上升接口联调成为…

作者头像 李华