惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
F
Full Disclosure
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
H
Hacker News: Front Page
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
B
Blog RSS Feed
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
Help Net Security
Help Net Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
W
WeLiveSecurity
T
Tenable Blog
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cyberwarzone
Cyberwarzone
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
O
OpenAI News
L
LINUX DO - 热门话题
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
J
Java Code Geeks
Know Your Adversary
Know Your Adversary
IT之家
IT之家
Latest news
Latest news
Cloudbric
Cloudbric

博客园 - soulsjie

winform窗体DataGridView合并单元格处理 C#GDI+阴影笔刷样式HatchStyle探讨 C#代码混淆工具ConfuserEx的使用 Aspose.Words在指定位置插入图片、调整图片大小 C#获取对象实体的键值对信息 C#将Winform上的TextBox和ComBox的值导入和导出 C#使用Aspose.Words将Spread表格插入到Word中 C# Aspose.Words将word中自定义的标签进行替换 Python文件操作基础方法 C# 将项目资源文件保存到磁盘上 SqlSugar数据库辅助类 使用存储过程备份MS SQLServer数据库 案例3:JAVA GUI 随机点名程序 案例2:JAVA GUI 简易计算器 ArcGIS JS API 添加要素图层 点击时获取图层属性 ArcGIS JS API 将天地图设置为底图 HTML5PLUS实现类似右侧弹出菜单 SqlSugar各数据库连接串 案例1:JAVAGUI用户管理
一种在winfrom窗体中显示计算公式的解决方案
soulsjie · 2026-03-17 · via 博客园 - soulsjie

前言

Winform窗体程序开发一些计算软件的时候,经常需要展示一些计算公式,之前都是用公式截取图片的形式展示到界面上。那么有没有一种方法可以直接将公式表达式在窗体程序中动态生成。

本文使用WpfMath,通过将变量动态拼接成LaTeX语法的公式字符串,并将公式转换成图片动态在窗体界面展示出来。

步骤

1.在Nuget安装WpfMath2.1.0

image

2.添加PresentationCore, PresentationFramework, WindowsBase的引用

image

image

3.封装方法将公式渲染为图片及调用

 1 public Form1()
 2 {
 3 InitializeComponent();
 4 string gs2 = @"A=\frac{\pi D^{2}}{4}=0.283\mathrm{m}^{2}";
 5 RenderMathFormula(gs2, pictureBox2);
 6 
 7 string gs1 = @"y=A\sin(\omega x+\varphi)+k";
 8 RenderMathFormula(gs1, pictureBox1);
 9 
10 }
11 
12 /// <summary>
13 /// 将 LaTeX 公式渲染为 WinForms 图片
14 /// </summary>
15 private void RenderMathFormula(string latex,PictureBox pictureBox)
16 {
17 Image image = null;
18 try
19 {
20 // 1. 获取解析器单例
21 var parser = WpfTeXFormulaParser.Instance;
22 // 2. 解析公式
23 var formula = parser.Parse(latex);
24 // 3. 创建WPF渲染环境
25 var environment = WpfTeXEnvironment.Create(
26 style: TexStyle.Display,
27 scale: 1,
28 systemTextFontName: "Microsoft YaHei"
29 );
30 // 4. 渲染为WPF位图
31 BitmapSource bitmapSource = formula.RenderToBitmap(environment);
32 // 5. 转换为 WinForms 可用的 Bitmap
33 using (var stream = new MemoryStream())
34 {
35 // 编码为 PNG
36 var encoder = new PngBitmapEncoder();
37 encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
38 encoder.Save(stream);
39 stream.Position = 0;
40 // 创建 WinForms Bitmap
41 var bitmap = new System.Drawing.Bitmap(stream);
42 // 显示在 PictureBox 中
43 image = bitmap;
44 }
45 }
46 catch (TexException ex)
47 {
48 MessageBox.Show($"公式语法错误: {ex.Message}", "渲染失败",MessageBoxButtons.OK, MessageBoxIcon.Warning);
49 }
50 catch (Exception ex)
51 {
52 MessageBox.Show($"公式渲染失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
53 }
54 pictureBox.Image=image;
55 pictureBox.SizeMode=PictureBoxSizeMode.AutoSize;
56 }
57 
58 private void button1_Click(object sender, EventArgs e)
59 {
60 string gs3 = @"y="+A.Text+@"\sin(" + ω.Text + @" x+" + φ.Text + @")+" + k.Text;
61 RenderMathFormula(gs3, pictureBox3);
62 }

image