首页 > 代码库 > Base64 Converter

Base64 Converter

<Window x:Class="Base64Convertor.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="387" Width="629">

<Grid>

<Grid.RowDefinitions>

<RowDefinition/>

<RowDefinition Height="auto"/>

<RowDefinition/>

</Grid.RowDefinitions>

<GroupBox Grid.Row="0" Header="Input" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="groupBox1" VerticalAlignment="Stretch">

<TextBox Name="txtInput" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" />

</GroupBox>

?

<TabControl Grid.Row="1">

<TabItem Header="Encode" >

<StackPanel Grid.Row="1" Orientation="Horizontal">

<Button Name="btnEncodeInputText" Content="From Input Text" Height="25" Margin="5" Click="btnEncodeInputText_Click" />

<Button Name="btnEncodeInputFile" Content="From Input File" Height="25" Margin="5" Click="btnEncodeInputFile_Click" />

<Button Name="btnCopyToClipboard" Content="Copy to Clipboard" Height="25" Margin="5" Click="btnCopyToClipboard_Click" />

</StackPanel>

</TabItem>

<TabItem Header="Decode">

<StackPanel Grid.Row="1" Orientation="Horizontal">

<Button Name="btnDecodeToOutput" Content="Decode to Output" Margin="5" Height="25" Click="btnDecodeToOutput_Click" />

<Button Name="btnDecodeToFile" Content="Decode to File" Height="25" Margin="5" Click="btnDecodeToFile_Click" />

</StackPanel>

</TabItem>

</TabControl>

?

<GroupBox Grid.Row="2" Header="Output" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="groupBox2" VerticalAlignment="Stretch">

<TextBox Name="txtOutput" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" />

</GroupBox>

</Grid>

</Window>

?

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

?

private void btnEncodeInputText_Click(object sender, RoutedEventArgs e)

{

txtOutput.Text = Convert.ToBase64String(Encoding.UTF8.GetBytes(txtInput.Text.Trim()));

}

?

private void btnDecodeToOutput_Click(object sender, RoutedEventArgs e)

{

txtOutput.Text = Encoding.UTF8.GetString(Convert.FromBase64String(txtInput.Text.Trim()));

}

?

private void btnEncodeInputFile_Click(object sender, RoutedEventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

if (ofd.ShowDialog().Value =http://www.mamicode.com/= true)

{

var fileContent = File.ReadAllBytes(ofd.FileName);

txtOutput.Text = Convert.ToBase64String(fileContent);

MessageBox.Show("done!");

}

}

?

private void btnCopyToClipboard_Click(object sender, RoutedEventArgs e)

{

Clipboard.SetText(txtOutput.Text);

}

?

private void btnDecodeToFile_Click(object sender, RoutedEventArgs e)

{

SaveFileDialog sfd = new SaveFileDialog();

if (sfd.ShowDialog().Value =http://www.mamicode.com/= true)

{

var fileContent = Convert.FromBase64String(txtInput.Text.Trim());

File.WriteAllBytes(sfd.FileName, fileContent);

MessageBox.Show("done!");

}

}

}

Base64 Converter