當前位置:編程學習大全網 - 編程語言 - C#編程 Base-64字符串中的無效字符

C#編程 Base-64字符串中的無效字符

Base-64字符串中的無效字符 指的是base64字符串轉化的格式是不正確的。

c#通常使用兩個方法來處理base64字符串

1:Convert.ToBase64String

2:Convert.FromBase64String

參考案例:將圖像轉化為base64字符串,再將base64字符串轉換為圖像

private void button4_Click(object sender, EventArgs e)

{

//處理圖像

string fileNmae = string.Empty;

OpenFileDialog openFileDialog1 = new OpenFileDialog();

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

fileNmae = openFileDialog1.FileName;

}

else

{

fileNmae = @"d:\對聯1.jpg";

}

Bitmap bmp = new Bitmap(fileNmae);

BinaryFormatter bin = new BinaryFormatter();

MemoryStream mem = new MemoryStream();

try

{

bin.Serialize(mem, bmp);

String strString = Convert.ToBase64String(mem.GetBuffer(), 0, Convert.ToInt32(mem.Length));

this.textBox1.Text = strString;

Application.DoEvents();

}

catch (Exception ex)

{

throw (ex);

}

finally

{

mem.Close();

}

}

private void button5_Click(object sender, EventArgs e)

{

byte[] bits = Convert.FromBase64String(this.textBox1.Text);

MemoryStream mem = new MemoryStream(bits);

BinaryFormatter bin = new BinaryFormatter();

try

{

object obj = ((object)(bin.Deserialize(mem)));

this.pictureBox1.Image = (Bitmap)obj;

}

catch (Exception ex)

{

throw (ex);

}

finally

{

mem.Close();

}

}

  • 上一篇:怎樣才能開始學習編程語言?
  • 下一篇:貼吧裏如何直接和某人聊天
  • copyright 2024編程學習大全網