实现简单的手写涂鸦板(demo源码)

简介: 在一些软件系统中,需要用到手写涂鸦的功能,然后可以将涂鸦的结果保存为图片,并可以将“真迹”通过网络发送给对方。这种手写涂鸦功能是如何实现的了?最直接的,我们可以使用Windows提供的GDI技术或GDI+技术来实现绘图功能。

     在一些软件系统中,需要用到手写涂鸦的功能,然后可以将涂鸦的结果保存为图片,并可以将“真迹”通过网络发送给对方。这种手写涂鸦功能是如何实现的了?最直接的,我们可以使用Windows提供的GDI技术或GDI+技术来实现绘图功能。但是,要实现一个如此简单的涂鸦板,也不是那么容易的事情。幸运的是,我们可以直接使用OMCS提供的内置集成了这种功能的一个WinForm控件HandwritingPanel

      HandwritingPanel控件的主要接口如下图所示:

        /// <summary>
        /// 设置画笔颜色。
         /// </summary>
        Color PenColor {set;}

        /// <summary>
        /// 设置画笔粗细。
         /// </summary>
        float PenWidth {set;}

        /// <summary>
        /// 清空画板。
         /// </summary>
        void Clear();

        /// <summary>
        /// 获取涂鸦的结果(位图)。
         /// </summary>       
        Bitmap GetHandWriting();

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

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

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

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

    public partial class HandwritingForm : Form
    {
        private Color currentColor = Color.Red;
        private List<float> penWidthList = new List<float>();
        private Bitmap currentImage;
        public Bitmap CurrentImage
        {
            get { return currentImage; }            
        }
        
        public HandwritingForm()
        {
            InitializeComponent();
          
            this.handwritingPanel1.PenColor = this.currentColor; //设置画笔颜色
            
              this.penWidthList.Add(2);
            this.penWidthList.Add(4);
            this.penWidthList.Add(6);
            this.penWidthList.Add(8);
            this.penWidthList.Add(10);
            this.comboBox_brushWidth.DataSource = this.penWidthList;
            this.comboBox_brushWidth.SelectedIndex = 1;
        }

        private void button_color_Click(object sender, EventArgs e)
        {
            try
            {
                this.colorDialog1.Color = this.currentColor;
                DialogResult result = this.colorDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    this.currentColor = this.colorDialog1.Color;
                    this.handwritingPanel1.PenColor = this.currentColor;   //设置画笔颜色                 
                }
            }
            catch (Exception ee)
            {              
                MessageBox.Show(ee.Message);
            }
        }

        //设置画笔宽度
        private void comboBox_brushWidth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox_brushWidth.SelectedIndex > 0)
            {
                this.handwritingPanel1.PenWidth = this.penWidthList[this.comboBox_brushWidth.SelectedIndex];
            }
            else
            {
                this.handwritingPanel1.PenWidth = this.penWidthList[0];
            }
        }

        private void Button_clear_Click(object sender, EventArgs e)
        {
            this.handwritingPanel1.Clear(); //清空手写板
        }

        private void button_Ok_Click(object sender, EventArgs e)
        {
            this.currentImage = this.handwritingPanel1.GetHandWriting(); //获取手写图片
              this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void Button_cancel_Click(object sender, EventArgs e)
        {           
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }        
    }

      其运行效果如下图所示:      

     

      下载demo源码

 

目录
相关文章
|
缓存 开发工具 git
报错:Git上传代码报错 will not add file alias already exists in index
报错:Git上传代码报错 will not add file alias already exists in index
624 0
|
算法 Java 程序员
内存回收
【10月更文挑战第9天】
456 5
|
缓存 Java 数据库
后端技术探索:从基础架构到高效开发的实践之路
【10月更文挑战第7天】 在现代软件开发中,后端技术是支撑应用运行的核心。本文将探讨如何从后端的基础架构出发,通过一系列高效的开发实践,提升系统的性能与可靠性。我们将深入分析后端框架的选择、数据库设计、接口开发等关键领域,并提供实用的代码示例和优化策略,帮助开发者构建更稳定、高效的后端系统。通过这篇文章,读者将获得关于后端开发的全面理解和实践指导,从而更好地应对复杂项目需求。
343 0
|
8月前
|
存储 文字识别 数据可视化
让你的电脑变得与众不同的冷门小工具
本文推荐了5款冷门但功能强大的小工具:1. MiniBin,轻量级回收站管理工具;2. AltairSimLab,多物理场仿真平台;3. 燃精灵,微信空号检测软件;4. IrfanView,经典图像查看器;5. Folder Size View,磁盘空间分析工具。这些软件体积小巧,功能出色,可大幅提升工作效率。
199 4
|
数据挖掘 Java 测试技术
无代码动态表单系统 毕业设计 JAVA+Vue+SpringBoot+MySQL(一)
无代码动态表单系统 毕业设计 JAVA+Vue+SpringBoot+MySQL
335 0
|
存储 传感器 人工智能
2024年非结构化数据管理将以四种方式发生变化
2024年非结构化数据管理将以四种方式发生变化
|
存储 JavaScript 前端开发
使用JS创造一个3D粒子化星空,十分酷炫,大家快进来看看吧
使用JS创造一个3D粒子化星空,十分酷炫,大家快进来看看吧
|
机器学习/深度学习 算法 数据挖掘
R语言在金融分析中扮演重要角色,用于风险管理、资产定价、量化交易、市场预测和投资组合优化。
【7月更文挑战第2天】R语言在金融分析中扮演重要角色,用于风险管理、资产定价、量化交易、市场预测和投资组合优化。其开源、强大的统计功能和丰富的包(如`PerformanceAnalytics`、`quantstrat`、`forecast`)支持从风险评估到策略回测的各种任务。R的灵活性和社区支持使其成为金融专业人士应对复杂问题的首选工具。
524 1
|
PHP 数据安全/隐私保护 计算机视觉
ThinkPHP图片处理之压缩图片大小,图片处理之图片水印(添加平铺文字水印,并设置文字之间的间距和文字的角度)
ThinkPHP图片处理之压缩图片大小,图片处理之图片水印(添加平铺文字水印,并设置文字之间的间距和文字的角度)
300 1
idea注释模版配置
idea注释模版配置
560 0