とある個人的な作業のために作りました。
クリップボードに入っている画像を、Base64エンコードしてくれるソフトを作りました。
Data URL の生成などに使えます。
public class TestApp
{
[System.STAThread]
public static void Main()
{
System.Windows.Forms.Application.Run(new MainForm());
}
}
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label e1;
private System.Windows.Forms.Button e2;
private System.Windows.Forms.ComboBox e3;
private System.Windows.Forms.TextBox e4;
private System.Windows.Forms.PictureBox e5;
private System.String e6;
public MainForm()
{
this.Width = 300;
this.Height = 500;
this.Text = "Base64 encode";
e1 = new System.Windows.Forms.Label();
e1.Width = 280;
e1.Height = 50;
e1.Top = 10;
e1.Left = 10;
e1.Text = "ボタンをクリックすると、クリップボードの内容をBase64エンコードした結果を表示します.";
this.Controls.Add(e1);
e2 = new System.Windows.Forms.Button();
e2.Text = "エンコードする";
e2.Width = 100;
e2.Top = 60;
e2.Left = 10;
e2.Click += e2_Click;
this.Controls.Add(e2);
e3 = new System.Windows.Forms.ComboBox();
e3.Width = 260;
e3.Top = 90;
e3.Left = 10;
e3.Items.AddRange(new object[] {
"",
"data:image/png;base64,",
"<img src=\"data:image/png;base64,"
});
e3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
e3.SelectedIndexChanged += e3_Changed;
this.Controls.Add(e3);
e4 = new System.Windows.Forms.TextBox();
e4.Width = 260;
e4.Height = 50;
e4.Top = 130;
e4.Left = 10;
e4.Multiline = true;
e4.WordWrap = false;
e4.Click += e4_Click;
this.Controls.Add(e4);
e5 = new System.Windows.Forms.PictureBox();
e5.Width = 260;
e5.Height = 260;
e5.Top = 190;
e5.Left = 10;
this.Controls.Add(e5);
}
private void e2_Click(object btn, System.EventArgs e)
{
System.Drawing.Image img = System.Windows.Forms.Clipboard.GetImage();
if (img == null ){
e4.Text = "クリップボードに画像がありませんでした";
e5.Image = null;
} else {
byte[] ImageBytes;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ImageBytes = ms.ToArray();
ms.Close();
}
e6 = System.Convert.ToBase64String(ImageBytes);
e5.Image = img;
e3_Changed(e3, new System.EventArgs());
}
}
private void e3_Changed(object btn, System.EventArgs e)
{
switch (e3.Text) {
case "data:image/png;base64,":
e4.Text = "data:image/png;base64," + e6;
break;
case "<img src=\"data:image/png;base64,":
e4.Text = "<img src=\"data:image/png;base64," + e6 + "\">";
break;
default:
e4.Text = e6;
break;
}
}
private void e4_Click(object btn, System.EventArgs e)
{
e4.SelectAll();
}
}