Friday, July 27, 2012

Windows Presentation Foundation #WPF Example: Base 64 encode/decode C#

This is a very basic example of a Windows Presentation Foundation application.In this application I encode a string to its base 64 equivalent string and then decode it back to the original string.

Open Visual Studio 2010 -> Create New Project -> Choose WPF Application




Once the project is created.Drag and drop two textboxes and two buttons to your MainWindow.xaml file


Double click the buttons and add the code below.

 private void button1_Click(object sender, RoutedEventArgs e)
 {
     textBox2.Text = Convert.ToBase64String(Encoding.Unicode.GetBytes(textBox1.Text));
 }

 private void button2_Click(object sender, RoutedEventArgs e)
 {
     textBox2.Text = Encoding.Unicode.GetString(Convert.FromBase64String(textBox1.Text));
  }

Compile the solution and run the program.

Output:




Source Code:

using System;
using System.Text;
using System.Windows;

namespace Base64EncodeDecode
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox2.Text = Convert.ToBase64String(Encoding.Unicode.GetBytes(textBox1.Text));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox2.Text = Encoding.Unicode.GetString(Convert.FromBase64String(textBox1.Text));
        }
    }
}






No comments: