//图片 转为 base64编码的文本
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true;
dlg.Title = "选择要转换的图片";
dlg.Filter = "Image files (*.jpg;*.bmp;*.gif;*.png)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";
if (DialogResult.OK == dlg.ShowDialog())
{
for (int i = 0; i < dlg.FileNames.Length; i++)
{
ImgToBase64String(dlg.FileNames[i].ToString());
}
}
}
//图片 转为 base64编码的文本
private void ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
this.pictureBox1.Image = bmp;
FileStream fs = new FileStream(Imagefilename + ".txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
String strbaser64 = Convert.ToBase64String(arr);
sw.Write(strbaser64);
sw.Close();
fs.Close();
// MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
MessageBox.Show("ImgToBase64String 转换失败
Exception:" + ex.Message);
}
}
//base64编码的文本 转为 图片
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true;
dlg.Title = "选择要转换的base64编码的文本";
dlg.Filter = "txt files|*.txt";
if (DialogResult.OK == dlg.ShowDialog())
{
for (int i = 0; i < dlg.FileNames.Length; i++)
{
Base64StringToImage(dlg.FileNames[i].ToString());
}
}
}
//base64编码的文本 转为 图片
private void Base64StringToImage(string txtFileName)
{
try
{
FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ifs);
String inputStr = sr.ReadToEnd();
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
//bmp.Save(txtFileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);
//bmp.Save(txtFileName + ".gif", ImageFormat.Gif);
//bmp.Save(txtFileName + ".png", ImageFormat.Png);
ms.Close();
sr.Close();
ifs.Close();
this.pictureBox2.Image = bmp;
if (File.Exists(txtFileName))
{
File.Delete(txtFileName);
}
//MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
MessageBox.Show("Base64StringToImage 转换失败
Exception:" + ex.Message);
}
}
//
public class base64Test5 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpPostedFile hpf = context.Request.Files["fileName"];
//将文件流写到byte数组中
Stream stream = hpf.InputStream;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
//将byte数组转为base64String
string base64String = Convert.ToBase64String(bytes);
//将base64String 转为 byte数组
byte[] byteArray = Convert.FromBase64String(base64String);
string saveFileName = DateTime.Now.ToFileTime().ToString();
string path = HttpContext.Current.Server.MapPath("../base/" + saveFileName + ".png");
//使用文件流读取byte数组中的数据
Stream s = new FileStream(path, FileMode.Append);
s.Write(byteArray, 0, byteArray.Length);
s.Close();
context.Response.Write("Hello World");
}
public bool IsReusable
{
get
{
return false;
}
}
}
//
Image fromImage = Image.FromFile( txtImg.Text );
MemoryStream stream = new MemoryStream();
fromImage.Save( stream, imgFormat[extension] );
txtString.Text = Convert.ToBase64String( stream.GetBuffer() );
MemoryStream stream = new MemoryStream( Convert.FromBase64String( txtString.Text ) );
bitmap img = new Bitmap( stream );
img.Save( txtImg.Text );
MessageBox.Show( "Completed!" );
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 Base64Demo
{
public partial class Form1 : Form
{
private string base64Str;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//
string path = textBox1.Text; //界面上第一个文件路径
string tempPath = textBox3.Text; //界面上第二个文件路径
FileStream filestream = new FileStream(path, FileMode.Open);
byte[] bt = new byte[filestream.Length];
//调用read读取方法
filestream.Read(bt, 0, bt.Length);
this.base64Str = Convert.ToBase64String(bt);
filestream.Close();
//将Base64串写入临时文本文件
if (File.Exists(tempPath))
{
File.Delete(tempPath);
}
FileStream fs = new FileStream(tempPath, FileMode.Create);
byte[] data = System.Text.Encoding.Default.GetBytes(this.base64Str);
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string outPath = textBox2.Text; //界面上第三个文件路径
var contents = Convert.FromBase64String(this.base64Str);
using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
fs.Write(contents, 0, contents.Length);
fs.Flush();
}
}
}
}