「C#」Base64形式の文字列と画像を相互変換するサンプルコード

サンプルコード
/// <summary>
/// 画像からBase64文字列に変換
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ImgToBase64(object sender, EventArgs e)
{
Image img = this.pictureBox.Image;
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream();
binFormatter.Serialize(memStream, img);
byte[] bytes = memStream.GetBuffer();
string base64 = Convert.ToBase64String(bytes);
this.richTextBox.Text = base64;
}

/// <summary>
/// Base64文字列から画像に変換
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Base64ToImage(object sender, EventArgs e)
{
string base64 = this.richTextBox.Text;
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
Image img = (Image)binFormatter.Deserialize(memStream);
this.pictureBox.Image = img;
}

C++

Posted by arkgame