在调用阿里云上面的api接口时,有些图片处理接口都需要将图片文件转换成base64,传递参数然后才能调用。
网上搜了下 一些图片和base转换的方法:
一、通过jQuery方法转换
html代码部分:
1 2 3
| <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代码部分 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <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代码部分:
1 2 3
| <asp:FileUpload ID="file_upload" runat="server" /> <asp:Button ID="btn" runat="server" Text="后台转换" OnClick="btn_Click" /> <img id="img_upload_show" src="" />
|
aps后台代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| /// <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所以会报错的 额 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| /// <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); } }
|