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AKKKKACiiigAooooAKKKKAP/Z
代码如下:
/* by 何问起
hovertree.com*/
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms; namespace HoverTree.Forms
{
public partial class FormBase64 : Form
{
public FormBase64()
{
InitializeComponent();
} private void button_decode_Click(object sender, EventArgs e)
{
try
{
pictureBox_hovertree.Image = ConvertStringToBitmap(textBox_hovertree.Text);
}
catch (Exception ex)
{ MessageBox.Show("解码失败。" + ex.Message,"HoverTree项目"); }
} Bitmap ConvertStringToBitmap(string base64string)
{
byte[] bt = Convert.FromBase64String(base64string);
MemoryStream h_stream = new MemoryStream(bt);
Bitmap h_bitmap = new Bitmap(h_stream); return h_bitmap;
} string ConvertBitmapToString(Bitmap bitmap)
{
MemoryStream h_m = new MemoryStream();
bitmap.Save(h_m, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] h_b = h_m.GetBuffer();
string base64string = Convert.ToBase64String(h_b); return base64string;
} private void button_open_Click(object sender, EventArgs e)
{
if (openFileDialog_hovertree.ShowDialog() == DialogResult.OK)
{
try
{
pictureBox_hovertree.Image = Image.FromFile(openFileDialog_hovertree.FileName);
}
catch (Exception ex)
{
MessageBox.Show("请选择图片文件。" + ex.Message);
}
}
} private void button_encode_Click(object sender, EventArgs e)
{
try
{
textBox_hovertree.Text = ConvertBitmapToString(new Bitmap(pictureBox_hovertree.Image));
}
catch (Exception ex)
{
MessageBox.Show("解码失败。" + ex.Message,"hovertree.com");
}
} private void button_zishu_Click(object sender, EventArgs e)
{
int h_length = textBox_hovertree.Text.Length;
int h_yushu = h_length % ;
MessageBox.Show("长度:"+h_length.ToString()+",除以4余数为"+h_yushu.ToString()+(h_yushu==?"": ",为非法base64编码"),"何问起");
}
}
}
转自:http://hovertree.com/h/bjaf/wa5nbd3w.htm