用Base64来传输照片,Base64与图片的互转 | Anforen.com

用Base64来传输照片,Base64与图片的互转
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace base64ImageFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

//读图片转为Base64String
System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(“d:/ctt.jpg”);
using (MemoryStream ms1 = new MemoryStream())
{
bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr1 = new byte[ms1.Length];
ms1.Position = 0;
ms1.Read(arr1, 0, (int)ms1.Length);
ms1.Close();
richTextBox1.Text = Convert.ToBase64String(arr1);
}
}

private void button2_Click(object sender, EventArgs e)
{

byte[] arr2 = Convert.FromBase64String(richTextBox1.Text);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
bmp2.Save(“d:/ctti.jpg”, System.Drawing.Imaging.ImageFormat.Jpeg);

}
}
}
}

(0)

相关推荐