實現簡單的手寫塗鴉板(demo源碼)


     在一些軟件系統中,需要用到手寫塗鴉的功能,然后可以將塗鴉的結果保存為圖片,並可以將“真跡”通過網絡發送給對方。這種手寫塗鴉功能是如何實現的了?最直接的,我們可以使用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源碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM