c#实现简单的手写板功能

   在一些输入法程序菜单中经常有手写板功能,这些功能如何实现呢?

最直接的,我们可以使用Windows提供的GDI技术或GDI+技术来实现绘图功能。但是,要实现一个如此简单的涂鸦板,也不是那么容易的事情。幸运的是,我们可以直接使用OMCS提供的内置集成了这种功能的一个WinForm控件HandwritingPanel

        HandwritingPanel控件的主要接口,代码

c#实现简单的手写板功能

 

 将HandwritingPanel控件从工具箱拖到你的UI上,可以通过PenColor和PenWidth属性设置画笔的颜色和粗细。运行起来后,就可以在控件的表面进行涂鸦和手写了。     

      如果需要清空手写板,则调用Clear方法。

      当手写结束的时候,则调用GetHandWriting方法得到手写的结果,并保存为位图。位图的大小即是HandwritingPanel控件的尺寸。

      OK,下面我们就写了一个使用HandwritingPanel来实现手写涂鸦板的demo,demo的主要代码如下所示


[csharp]  view plain copy
  1. public partial class HandwritingForm : Form    
  2. {    
  3.     private Color currentColor = Color.Red;    
  4.     private List<float> penWidthList = new List<float>();    
  5.     private Bitmap currentImage;    
  6.     public Bitmap CurrentImage    
  7.     {    
  8.         get { return currentImage; }                
  9.     }    
  10.         
  11.     public HandwritingForm()    
  12.     {    
  13.         InitializeComponent();    
  14.           
  15.         this.handwritingPanel1.PenColor = this.currentColor; //设置画笔颜色    
  16.             
  17.           this.penWidthList.Add(2);    
  18.         this.penWidthList.Add(4);    
  19.         this.penWidthList.Add(6);    
  20.         this.penWidthList.Add(8);    
  21.         this.penWidthList.Add(10);    
  22.         this.comboBox_brushWidth.DataSource = this.penWidthList;    
  23.         this.comboBox_brushWidth.SelectedIndex = 1;    
  24.     }    
  25.     
  26.     private void button_color_Click(object sender, EventArgs e)    
  27.     {    
  28.         try    
  29.         {    
  30.             this.colorDialog1.Color = this.currentColor;    
  31.             DialogResult result = this.colorDialog1.ShowDialog();    
  32.             if (result == DialogResult.OK)    
  33.             {    
  34.                 this.currentColor = this.colorDialog1.Color;    
  35.                 this.handwritingPanel1.PenColor = this.currentColor;   //设置画笔颜色                     
  36.             }    
  37.         }    
  38.         catch (Exception ee)    
  39.         {                  
  40.             MessageBox.Show(ee.Message);    
  41.         }    
  42.     }    
  43.     
  44.     //设置画笔宽度    
  45.     private void comboBox_brushWidth_SelectedIndexChanged(object sender, EventArgs e)    
  46.     {    
  47.         if (this.comboBox_brushWidth.SelectedIndex > 0)    
  48.         {    
  49.             this.handwritingPanel1.PenWidth = this.penWidthList[this.comboBox_brushWidth.SelectedIndex];    
  50.         }    
  51.         else    
  52.         {    
  53.             this.handwritingPanel1.PenWidth = this.penWidthList[0];    
  54.         }    
  55.     }    
  56.     
  57.     private void Button_clear_Click(object sender, EventArgs e)    
  58.     {    
  59.         this.handwritingPanel1.Clear(); //清空手写板    
  60.     }    
  61.     
  62.     private void button_Ok_Click(object sender, EventArgs e)    
  63.     {    
  64.         this.currentImage = this.handwritingPanel1.GetHandWriting(); //获取手写图片    
  65.           this.DialogResult = System.Windows.Forms.DialogResult.OK;    
  66.     }    
  67.     
  68.     private void Button_cancel_Click(object sender, EventArgs e)    
  69.     {               
  70.         this.DialogResult = System.Windows.Forms.DialogResult.Cancel;    
  71.     }            
  72. }    
其运行效果如下图所示:c#实现简单的手写板功能

在vc+开发输入法经常需要gdi技术开发手写功能

end,试着练习一下。

当然也可以在网页上实现手写输入功能,类似于百度在线手写输入。

你可能感兴趣的:(c#实现简单的手写板功能)