Paint Brush Application Using HTML5 Canvas


Best way of making a software application is to "develop a web-based application and host it on Cloud", then we don't have to worry about installations, as it will be easily updatable and easy to manage versions. Also our application can support different platforms like Windows, Linux, iOS, Android, etc... Only thing you have to test it with different browsers.

Please visit the link below to get started with a Paintbrush Application using HTML5 Canvas.

Style button with image, in WPF

The first thing is, we have to make a custom Button control

*Note : Your application can be named "ImageButtonSample"


namespace ImageButtonSample
    public class ImageButton : Button
        private ImageSource _myImage;

        public ImageSource MyImage
            get { return _myImage; }
            set { _myImage = value; }

Next we have to define the style for this button.


 <ResourceDictionary xmlns=""
    <!--  A button style that displays an attached image -->
    <Style TargetType="{x:Type local:ImageButton}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="ContentTemplate">
                            Source="{Binding Path=(local:ImageButton.MyImage),
                            RelativeSource={RelativeSource FindAncestor,
                            AncestorType={x:Type Button}}}"
                            HorizontalAlignment="Left" Margin="8,0,0,0"                             
                            Height="16" Width="16" />
                            Text="{TemplateBinding Content}"
                            HorizontalAlignment="Center" />

The new stylish image button is ready. Now we can use it in our application.

*Note : Don't forget to add the resource dictionary reference in your client application

 <Window x:Class="ImageButtonSample.Window1"
    Title="Window1" Height="300" Width="300">
        <ResourceDictionary Source="ImageButton.xaml"/>
        <local:ImageButton MyImage="apply.png"
                           Content="Apply" Margin="89,116,63,116" />

Serial Port Communication

System.IO.Ports - namespace contains a class named "SerialPort".

This class facilitate us to do the communication with serial [COM] ports.

How to open a serial port ?

SerialPort serialPort = new SerialPort(); 

//configuring the serial port
serialPort .PortName = "COM1";
serialPort .BaudRate = 9600;
serialPort .DataBits = 8;
serialPort .Parity = Parity.None;
serialPort .StopBits = StopBits.One;

//opening the serial port
serialPort .Open();

Open() function calling is actually opening the COM port. So this call can be inside a try catch block to capture the errors while opening.

 How to send data  to serial port?

There are two functions for this purpose.

1.  serialPort.Write();  //receives text or byte[] with offset and count

2. serialPort.WriteLine() //receives text 

 How to read data from serial port?

Handle the DataReceived event of SerialPort class.

serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); 

void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
     string data = serialPort.ReadExisting();

 To test the serial port communication applications, we can use the "Virtual Serial Port Emulator" if you does'nt have the actual communication device.

Read password in Console Application

I had a situation to read a password using console application. System.Console doesn't allow to read a password as like in winforms. So we have to manipulate the same in Console.

        public static string ReadPassword()
            string password = "";
            ConsoleKeyInfo info;

                info = Console.ReadKey(true); //Read key without echo

                if (info.Key == ConsoleKey.Backspace)
                    if (!string.IsNullOrEmpty(password))
                        password = password.Substring(0, password.Length - 1);
                    //Print backspace, clear the character by overwrite, again backspace
                    Console.Write("\b" + " " + "\b");
                    password += info.KeyChar;
            while (info.Key != ConsoleKey.Enter);

            return password;

Then you can use this function

string _password = ReadPassword();