/// <summary> /// 将图片数据转换为Base64字符串 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToBase64( 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 ToImage( 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; } /// <summary> /// 显示头像 /// </summary> /// <param name="imageBytes">图片二进制流</param> public void PictureShow(byte[] imageBytes) { if (imageBytes != null) { //读取二进制转换成图片 MemoryStream ms = new MemoryStream(imageBytes); Bitmap bmpt = new Bitmap(ms); pictureDocImg.Image = bmpt; pictureDocImg.SizeMode = PictureBoxSizeMode.Zoom; } else { pictureDocImg.Image = null; } } /// <summary> /// 将Base64字符串转换为图片 /// </summary> /// <param name="pictureBox">图片控件</param> /// <param name="base64String">base64位图片字符串</param> public static void ToImage(PictureBox pictureBox, string base64String) { string base64 = base64String;//"data:image/jpg;base64," byte[] bytes = Convert.FromBase64String(base64); MemoryStream memStream = new MemoryStream(bytes, 0, bytes.Length); memStream.Position = 0; Bitmap bmp = new Bitmap(memStream); pictureBox.Image = bmp; } |