Implement simple handwriting graffiti (demo source code)

Source: Internet
Author: User

In some software systems, you need to use the handwriting graffiti function. Then you can save the graffiti results as images and send "authentic" to each other through the network. How is this handwritten graffiti function implemented? Most directly, we can use the GDI or GDI + technology provided by Windows to implement the drawing function. However, it is not that easy to implement such a simple graffiti. Fortunately, we can directly use a WinForm control provided by OMCS with built-in integration of this function.HandwritingPanel.

Shows the main interfaces of the HandwritingPanel control:

/// <Summary> /// set the paint brush color. /// </Summary> Color PenColor {set ;}/// <summary> /// set the paint brush width. /// </Summary> float PenWidth {set ;}/// <summary> // clear the canvas. /// </Summary> void Clear (); // <summary> // obtain the graffiti result (Bitmap ). /// </Summary> Bitmap GetHandWriting ();

Drag the HandwritingPanel control from the toolbox to your UI. You can use the PenColor and PenWidth attributes to set the color and width of the paint brush. After running, you can graffiti and handwriting on the control surface.

To Clear a tablet, call the Clear method.

When the handwriting ends, call the GetHandWriting method to obtain the handwritten result and save it as a bitmap. The bitmap size is the size of the HandwritingPanel control.

OK. Now we have written a demo using HandwritingPanel to implement handwriting graffiti. The main code of the demo is as follows:

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; // set the paint brush color 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; // set the paint brush color} catch (Exception ee) {MessageBox. show (ee. message) ;}}// set the paint width 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 (); // clear the tablet} private void button_ OK _Click (object sender, EventArgs e) {this. currentImage = this. handwritingPanel1.GetHandWriting (); // obtain the handwritten image this. dialogResult = System. windows. forms. dialogResult. OK;} private void Button_cancel_Click (object sender, EventArgs e) {this. dialogResult = System. windows. forms. dialogResult. cancel ;}}

Shows the running effect:

Download the demo source code.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.