本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式。具体思路如下:

(1)通过TextBox获取用户输入的LaTeX数学表达式,然后对表达式格式化使之便于网络传输。

(2)将格式化之后的字符串,通过Http请求发送至codecogs.com或者Google.com。

(3)获取网站返回的数据流,将其转化为图片,并显示在PictureBox上。

具体过程为:

首先,我们在这个网站输入LaTeX数学公式然后返回图片时,即“http://latex.codecogs.com/gif.latex?“后面跟上我们输入的公式内容。比如”http://latex.codecogs.com/gif.latex?\alpha”就显示一个希腊字母。所以我们可以在其后加上我们希望转换的公式即可。但是需要注意的是,网络URL中的空格有时候会自动转化为加号”+“。所以,我们在传输的时候需要将空格去掉。或者将其转换为”%20“。

建立如图所示的Form。一个TextBox,六个Button和一个PictureBox。

用例为著名的“薛定谔方程”:

i\hbar\frac{\partial \psi}{\partial {t}} = \frac{-\hbar^2}{2m} \left( \frac{\partial^2}{\partial {x^2}} + \frac{\partial^2}{\partial {y^2}} + \frac{\partial^2}{\partial {z^2}} \right) \psi + V \psi

“粘贴文本”按钮添加如下单击事件。

           private void btnPasteText_Click(object sender, EventArgs e)
{
string content = Clipboard.GetText(); // 获取剪切板文本信息
textBox.Text = content; // 将信息显示到TextBox
}
private bool check()
{
if(textBox.Text.Trim() == "") // 如果TextBox为空
{
MessageBox.Show(this, "请填写 LaTeX 函数代码!");
return false;
}
return true;
}

“Google预览”按钮添加如下事件。

           private void btnPreviewGoogle_Click(object sender, EventArgs e)
{
if (check())
{
// 首先将文本信息格式化,作为URL信息。
string ImgUrl = String.Format(PicUrlGoogle, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl; // 加载网络图片到PictureBox
btnCopyImg.Enabled = true; // 使“复制图像”按钮可用
}
else
btnCopyImg.Enabled = false; // 否则使“复制图像”按钮不可用
}

“Cogs预览”按钮添加如下事件。

         private void btnPreviewCogs_Click(object sender, EventArgs e)
{
if (check())
{
// 首先将文本信息格式化,作为URL信息。
string ImgUrl = String.Format(PicUrlCogs, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl; // 加载网络图片到PictureBox
btnCopyImg.Enabled = true; // 使“复制图像”按钮可用
}
else
btnCopyImg.Enabled = false; // 否则使“复制图像”按钮不可用
}

“复制图像”按钮添加如下单击事件。

        private void btnCopyImg_Click(object sender, EventArgs e)
{
if(pictureBox.Image != null)
Clipboard.SetImage(pictureBox.Image); // 将Picture图片复制到剪切板
}

“显示帮助”按钮添加如下事件。

        private void btnHelp_Click(object sender, EventArgs e)
{
textBox.Text = "1、LaTex 公式前后无需 $ 符号;\r\n"
+ "2、需要联网,Google丑,Cogs慢;\r\n"
+ "3、尽量多使用 {} 将字段括起来;\r\n"
+ "4、于 2015年11月13日。";
}

“退出”按钮添加如下事件。

        private void btnExit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0); // 退出程序
}

完整代码如下:

using System;
using System.Windows.Forms;
using System.Web; namespace LaTeX_Win
{
public partial class Form1 : Form
{
private static string PicUrlGoogle = @"http://chart.apis.google.com/chart?cht=tx&chl={0}";
private static string PicUrlCogs = @"http://latex.codecogs.com/gif.latex?{0}";
public Form1()
{
InitializeComponent();
} private void btnPasteText_Click(object sender, EventArgs e)
{
string content = Clipboard.GetText();
textBox.Text = content;
} private void btnPreviewGoogle_Click(object sender, EventArgs e)
{
if (check())
{
string ImgUrl = String.Format(PicUrlGoogle, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl;
btnCopyImg.Enabled = true;
}
else
btnCopyImg.Enabled = false;
}
private void btnPreviewCogs_Click(object sender, EventArgs e)
{
if (check())
{
string ImgUrl = String.Format(PicUrlCogs, HttpUtility.UrlPathEncode(textBox.Text));
pictureBox.ImageLocation = ImgUrl;
btnCopyImg.Enabled = true;
}
else
btnCopyImg.Enabled = false;
}
private void btnCopyImg_Click(object sender, EventArgs e)
{
if(pictureBox.Image != null)
Clipboard.SetImage(pictureBox.Image);
}
private bool check()
{
if(textBox.Text.Trim() == "")
{
MessageBox.Show(this, "请填写 LaTeX 函数代码!");
return false;
}
return true;
} private void btnExit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
} private void btnHelp_Click(object sender, EventArgs e)
{
textBox.Text = "1、LaTex 公式前后无需 $ 符号;\r\n"
+ "2、需要联网,Google丑,Cogs慢;\r\n"
+ "3、尽量多使用 {} 将字段括起来;\r\n"
+ "4、于 2015年11月13日。";
}
}
}

C#开发基于Http的LaTeX数学公式转换器的更多相关文章

  1. Android开发手记(29) 基于Http的LaTeX数学公式转换器

    本文将讲解如何通过codecogs.com和Google.com提供的API接口来将LaTeX数学函数表达式转化为图片形式.具体思路如下: (1)通过EditText获取用户输入的LaTeX数学表达式 ...

  2. Spring7——开发基于注解形式的spring

    开发基于注解形式的spring SpringIOC容器的2种形式: (1)xml配置文件:applicationContext.xml; 存bean:<bean> 取bean: Appli ...

  3. [Intel Edison开发板] 05、Edison开发基于MRAA实现IO控制,特别是UART通信

    一.前言 下面是本系列文章的前几篇: [Intel Edison开发板] 01.Edison开发板性能简述 [Intel Edison开发板] 02.Edison开发板入门 [Intel Edison ...

  4. {VS2010C#}{WinForm}{ActiveX}VS2010C#开发基于WinForm的ActiveX控件

    在VS2010中使用C#开发基于WinForm的ActiveX控件 常见的一些ActiveX大部分是使用VB.Delphi.C++开发,使用C#开发ActiveX要解决下面三个问题: 使.NET组件可 ...

  5. markdown下编辑latex数学公式

    在利用为知笔记编写笔记的时候,有时需要用的markdown,只要把文件名加上后缀.md,就可以使用markdown语法,以下介绍在markdown下编辑latex数学公式. 使用LaTeX写公式的基本 ...

  6. Form_Form Builder开发基于视图页面和自动代码生成包(案例)

     2014-01-06 Created By BaoXinjian

  7. 转】Mahout分步式程序开发 基于物品的协同过滤ItemCF

    原博文出自于: http://blog.fens.me/hadoop-mahout-mapreduce-itemcf/ 感谢! Posted: Oct 14, 2013 Tags: Hadoopite ...

  8. Markdown 添加 Latex 数学公式

    添加公式的方法 Latex 数学公式语法 添加公式的方法 行内公式 $行内公式$ 行间公式 $$行间公式$$ Latex 数学公式语法 角标(上下标) 上标命令^{} 下标命令_{} 上下标命令用来放 ...

  9. 最简单的基于FFMPEG的封装格式转换器(无编解码)

    本文介绍一个基于FFMPEG的封装格式转换器.所谓的封装格式转换,就是在AVI,FLV,MKV,MP4这些格式之间转换(相应.avi,.flv,.mkv,.mp4文件).须要注意的是,本程序并不进行视 ...

随机推荐

  1. 【HDOJ】3029 Scales

    CF上有道类似的,做了那个这个简单多了.思路是取模.模等于1如何处理,模等于2如何分类分类讨论后.可解.解得对数据排序后再输出. /* 3029 */ #include <iostream> ...

  2. 【HDOJ】1892 See you~

    wa了十次,原来变量名写错.二维树状数组. #include <cstdio> #include <cstring> #define MAXN 1002 int nums[MA ...

  3. Response.ContentType 详细列表 <转>

    Response.ContentType 详细列表   不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% ...

  4. mysql中的timestamp类型时间比较:unix_timestamp函数

    在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...

  5. [PHP] 跳转以及回到原来的地址

    回到原来的地址: 1.PHP(PHP代码) Header('Location:'.$_SERVER["HTTP_REFERER"]); 2.JavaScript(相当于后退按钮,- ...

  6. Unity3d 真实的植物渲染

    好久没写shader了,有些生疏,刚弄了个植物shader,分享一下. 先上图片: 重点需要注意的是fragment shader的透明部分 需要如此声明 Tags{ "LightMode& ...

  7. jboss as7 o.h.c.s.c.i.BroadcastGroupImpl Network is unreachable

    Question: [Server:server-one] 22:52:56,876 ERROR [org.hornetq.core.server.cluster.impl.BroadcastGrou ...

  8. 《A First Course in Probability》-chaper7-期望的性质-期望的性质-协方差

    在实际的问题中,我们往往想要通过已有的数据来分析判断两个事件的发生是否有相关性.当然一个角度去寻找这两个事件内在的逻辑关系,这个角度需要深究两个事件的本质,而另外一个角度就是概率论提供的简单方法:基于 ...

  9. hdoj 2277 Change the ball【找规律】

    Change the ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. hdu 1242 dfs/bfs

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...