1.图表转换成BASE64码
private string buffer1;
private string buffer2;
//读取文件...
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "图形文件(*.txt)|*.gif";
openDlg.FileName ="";
openDlg.DefaultExt = ".gif";
openDlg.CheckFileExists = true;
openDlg.CheckPathExists = true;
DialogResult res = openDlg.ShowDialog();
if (res == DialogResult.OK)
{
for (int i = 0; i < openDlg.FileNames.Length; i++)
{
buffer1 = openDlg.FileNames[i].ToString();
}
button2.Enabled = true;
}
}
//开始转换...
public void EncodeWithString()
{
System.IO.FileStream inFile;
byte[] binaryData;
try
{
inFile = new System.IO.FileStream(buffer1,System.IO.FileMode.Open,System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,(int)inFile.Length);
inFile.Close();
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
return;
}
try
{
buffer2 = System.Convert.ToBase64String(binaryData,0,binaryData.Length);
}
catch (System.ArgumentNullException)
{
System.Console.WriteLine("Binary data array is null.");
return;
}
System.IO.StreamWriter outFile;
try
{
outFile = new System.IO.StreamWriter(buffer1,false,System.Text.Encoding.ASCII);
outFile.Write(buffer2);
outFile.Close();
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
}
}
//转换并保存文件
private void button2_Click(object sender, EventArgs e)
{
EncodeWithString();
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "文本文件(*.txt)|*.txt";
saveDlg.DefaultExt = ".txt";
saveDlg.FileName = "";
DialogResult res = saveDlg.ShowDialog();
if (res == DialogResult.OK)
{
System.IO.FileStream s = new System.IO.FileStream (saveDlg.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.StreamWriter w = new System.IO.StreamWriter(s);
w.WriteLine(buffer2);
w.Close();
s.Close();
MessageBox.Show("转换成功!");
buffer1 = "";
buffer2 = "";
}
}
2.BASE64码转换成图片
private string buffer1;
private string buffer2;
private string buffer3;
//读取文件...
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Filter = "文本文件(*.txt)|*.txt";
openDlg.FileName ="";
openDlg.DefaultExt = ".txt";
openDlg.CheckFileExists = true;
openDlg.CheckPathExists = true;
DialogResult res = openDlg.ShowDialog();
if (res == DialogResult.OK)
{
for (int i = 0; i < openDlg.FileNames.Length; i++)
{
buffer1 = openDlg.FileNames[i].ToString();
System.IO.StreamReader srRead;
srRead = new System.IO.StreamReader((System.IO.Stream)System.IO.File.OpenRead(openDlg.FileName), System.Text.Encoding.Default);
srRead.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);
while (srRead.Peek() > -1)
{
buffer2 += srRead.ReadLine();
}
srRead.Close();
}
}
}
//开始转换...
public void DecodeWithString()
{
System.IO.StreamReader inFile;
string base64String;
try
{
char[] base64CharArray;
inFile = new System.IO.StreamReader(buffer1,System.Text.Encoding.ASCII);
base64CharArray = new char[inFile.BaseStream.Length];
inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
base64String = new string(base64CharArray);
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
return;
}
try
{
binaryData = System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException)
{
System.Console.WriteLine("Base 64 string is null.");
return;
}
catch (System.FormatException)
{
System.Console.WriteLine("Base 64 string length is not " +"4 or is not an even multiple of 4.");
return;
}
try
{
System.IO.FileStream outFile;
outFile = new System.IO.FileStream(buffer3,System.IO.FileMode.Create,System.IO.FileAccess.Write);
outFile.Write(binaryData, 0, binaryData.Length);
outFile.Close();
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
}
}
//转换并保存文件
private void button2_Click(object sender, EventArgs e)
{
DecodeWithString();
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "图形文件(*.gif)|*.gif";
saveDlg.DefaultExt = ".gif";
saveDlg.FileName = "";
DialogResult res = saveDlg.ShowDialog();
if (res == DialogResult.OK)
{
buffer3 = saveDlg.FileName;
//panel1.BackgroundImage = Image.FromStream(binaryData);
}
}