网上多的是:
using System;using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemIO;
using SystemDrawingImaging;
namespace base64_img
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 转为 base64编码的文本
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlgTitle = "选择要转换的";
dlgFilter = "Image files (jpg;bmp;gif)|jpgjpeg;gif;bmp|AllFiles ()|";
if (DialogResultOK == dlgShowDialog())
{
ImgToBase64String(dlgFileName);
}
}
// 转为 base64编码的文本
private void ImgToBase64String(string Imagefilename)
{
try
{
Bitmap bmp = new Bitmap(Imagefilename);
thispictureBox1Image = bmp;
FileStream fs = new FileStream(Imagefilename + "txt", FileModeCreate);
StreamWriter sw = new StreamWriter(fs);
MemoryStream ms = new MemoryStream();
bmpSave(ms, SystemDrawingImagingImageFormatJpeg);
byte[] arr = new byte[msLength];
msPosition = 0;
msRead(arr, 0, (int)msLength);
msClose();
String strbaser64 = ConvertToBase64String(arr);
swWrite(strbaser64);
swClose();
fsClose();
MessageBoxShow("转换成功!");
}
catch (Exception ex)
{
MessageBoxShow("ImgToBase64String 转换失败/nException:" + exMessage);
}
}
//base64编码的文本 转为
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlgTitle = "选择要转换的base64编码的文本";
dlgFilter = "txt files|txt";
if (DialogResultOK == dlgShowDialog())
{
Base64StringToImage(dlgFileName);
}
}
//base64编码的文本 转为
private void Base64StringToImage(string txtFileName)
{
try
{
FileStream ifs = new FileStream(txtFileName, FileModeOpen, FileAccessRead);
StreamReader sr = new StreamReader(ifs);
String inputStr = srReadToEnd();
byte[] arr = ConvertFromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
bmpSave(txtFileName + "jpg", SystemDrawingImagingImageFormatJpeg);
//bmpSave(txtFileName + "bmp", ImageFormatBmp);
//bmpSave(txtFileName + "gif", ImageFormatGif);
//bmpSave(txtFileName + "png", ImageFormatPng);
msClose();
srClose();
ifsClose();
thispictureBox1Image = bmp;
MessageBoxShow("转换成功!");
}
catch (Exception ex)
{
MessageBoxShow("Base64StringToImage 转换失败/nException:"+exMessage);
}
}
}
}
<pre></pre>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)