您的位置:首页 > 编程语言 > ASP

【asp】图片和Base64编码互相转换

2017-08-10 17:26 495 查看
在调用阿里云上面的api接口时,有些图片处理接口都需要将图片文件转换成base64,传递参数然后才能调用。

网上搜了下 一些图片和base转换的方法:

一、通过jQuery方法转换

html代码部分:
<input type="file" name="file" id="img_upload_file" value="" multiple="multiple" />
<input type="hidden" name="img_upload_base" id="img_upload_base" />
<textarea rows="30" cols="100" id="img_upload_base_text"></textarea>
js代码部分 :
<script src="scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function () {
$("#img_upload_file").change(function () {
var file = this.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);//调用自带方法进行转换
reader.onload = function (e) {
$("#img_upload_show").attr("src", this.result);//将转换后的编码存入src完成预览
$("#img_upload_base").val(this.result);//将转换后的编码保存到input供后台使用
$("#img_upload_base_text").val(this.result);//显示出  转换后的编码数据
};
});
});
</script>
这个方法比较简单,省时省力,转换的操作都放到用户浏览器上进行,减少服务器压力。

二 、asp后台处理转换 

html代码部分:
<asp:FileUpload ID="file_upload" runat="server" />
<asp:Button ID="btn" runat="server" Text="后台转换" OnClick="btn_Click" />
<img id="img_upload_show" src="" />


aps后台代码部分:
protected void btn_Click(object sender, EventArgs e)
{
//获取上传 文件的名称
string btn_file=  file_upload.PostedFile.FileName;
//获取 服务器上路径
string imgPath = Server.MapPath("Imagers/");
//获取 文件后缀
string houzui = btn_file.Substring(btn_file.LastIndexOf('.'));
//上传文件 路径 ,重命名
string filePath = imgPath + Guid.NewGuid().ToString()+ houzui;
//上传文件
file_upload.SaveAs(filePath);

//转换base64
string result = imgToBase64(filePath);

//写入txt文件中
string path = Server.MapPath("/Base");
System.IO.File.WriteAllText(path + "//" + Guid.NewGuid().ToString() + ".txt", result);
}


imagerToBase64的方法:
/// <summary>
/// imager 转换 成Base64
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>base64编码</returns>
private string imgToBase64(string filePath)
{
//用streamread读这个文件
System.IO.StreamReader sr = new StreamReader(filePath, Encoding.Default, true);

int index;
//实例化一个内存流
System.IO.MemoryStream tempStream = new MemoryStream();
//将流转换为字节数组
while ((index = sr.BaseStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)index));
}
byte[] array = tempStream.ToArray();
tempStream.Close();
//将得到的字节数组转换为base64位编码
string result = Convert.ToBase64String(array);
return result;
}
这个方法不太方便,需要先将图片上传至服务器,然后得到图片的路径,再进行 转换。

三、Base64编码转图片

在写这个时候,遇到个bug,Bitmap参数无效,原来是错误的将  gif 的 动态图的编码转成Bitmap所以会报错的 额 。

/// <summary>
/// base64编码的文本 转为    图片
/// </summary>
/// <param name="txtFileName">Base编码的txt文本路径 </param>
private void Base64StringToImage(string txtFileName)
{
try
{
FileStream ifs = new FileStream(txtFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(ifs);
//读取txt里面的内容
String inputStr = sr.ReadToEnd();

//转图片
byte[] bt = Convert.FromBase64String(inputStr);
System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
Bitmap bmp = new Bitmap(stream);

string fileName = txtFileName.Substring(0, txtFileName.IndexOf("."));

if (File.Exists(fileName))
{
File.Delete(fileName);
}
//存储到服务器 上
bmp.Save(fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Save(fileName + ".bmp", ImageFormat.Bmp);
bmp.Save(fileName + ".gif", ImageFormat.Gif);
bmp.Save(fileName + ".png", ImageFormat.Png);
stream.Close();
sr.Close();
ifs.Close();
}
catch (Exception ex)
{
//("Base64StringToImage 转换失败\nException:" + ex.Message);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: