码迷,mamicode.com
首页 > Windows程序 > 详细

C#保存Base64格式图片

时间:2016-03-11 16:50:09      阅读:497      评论:0      收藏:0      [点我收藏+]

标签:

  1 1.前端页面代码
  2 /**
  3 * 通过图片本地路径获取图片真实大小,并进行压缩
  4 */
  5 function getLocalRealSize(path, callback) {
  6     var img = new Image();
  7     var tempimg = new Image();
  8     img.src = path;
  9     img.onload = function() {
 10         var height = img.naturalHeight;
 11         var width = img.naturalWidth;
 12         img.width = img.naturalWidth;
 13         img.height = img.naturalHeight;
 14         //压缩图片
 15         var canvas = compressImage(img);
 16         tempimg = canvas.toDataURL(image/jpeg);
 17         
 18         callback(tempimg);
 19     } 
 20 }; 
 21 /**
 22 * 压缩图片
 23 */
 24 function compressImage(image) {
 25         var canvas = document.createElement("canvas");
 26         var mwidth = image.width;
 27         var mheight = image.height;
 28         var scale = 1;
 29         var i = 0;
 30         while (true) {
 31             if (mwidth >> i <= 1000 && mheight >> i <= 1000) {
 32                 scale = Math.pow(0.5, i);
 33                 break;
 34             }
 35             i += 1;
 36         }
 37         canvas.width = mwidth * scale;
 38         canvas.height = mheight * scale;
 39         canvas.getContext("2d").drawImage(image, 0, 0, mwidth * scale, mheight * scale);
 40         return canvas;
 41     }
 42     //主页面   车主首页    跳转
 43     document.getElementById("driverindex").addEventListener("tap", function() {
 44         //获取img对象
 45         var OwerImg = document.getElementById("owner-photo-img");
 46         var OwerCardImg = document.getElementById("owner-cardphoto-img");
 47         var OwerLicenceImg = document.getElementById("owner-driver1-img");
 48         var OwerDrivingLicenceImg = document.getElementById("owner-driver2-img");
 49         var CarImg = document.getElementById("owner-car-img");
 50         var AssuranceImg = document.getElementById("owner-sure-img");
 51         
 52         var oArray=source.split("|"); 
 53         var totalCount=imgCount+oArray.length-1;
 54         if(totalCount<6){
 55             mui.toast("请上传完整信息");
 56             return false; 
 57         }
 58         uploadImg();
 59     });
 60 });
 61 // 上传文件
 62 var uploadImg = function() {
 63     var server = imgUrl+/clientapi/driver/ApplayCertification;
 64     //outSet("开始上传:")
 65     var wt = plus.nativeUI.showWaiting();
 66     var task = plus.uploader.createUpload(server, {
 67             method: "POST",
 68             blocksize: 20480000,
 69             priority: 100
 70         },
 71         function(t, status) { //上传完成
 72             if (t.state == 4 && status == 200) {
 73                 wt.close();
 74                 //outLine("上传成功:" + t.responseText);
 75                 //上传成功,跳转到车主首页
 76                 mui.toast(上传成功);
 77                 mui.fire(mycarPage, "pageflowrefresh" );
 78                 plus.webview.currentWebview().close();
 79                 mui.redirect(myaccount-mycar, ../myaccount/mycar.html);
 80             } else {
 81                 //outLine("上传失败:" + status);
 82                 wt.close();
 83                 mui.toast(上传失败);
 84             }
 85         }
 86     );
 87     task.addData("token", UserInfo.token());
 88     task.addData("source", source);
 89     task.start(); 
 90 }
 91 2.C#后台代码
 92  //将base64编码的字符串转为图片并保存
 93 protected string SaveBase64Image(string source, string path, int userID)
 94 {
 95     try
 96     {
 97         var now = DateTime.Now;
 98         string filePath = path + now.ToString("yyyy-MM-dd") + "/" + userID.ToString() + "/";
 99         string fileName = now.ToString("yyyyMMddhhmmssffff") + ".jpg";
100         Log.Debug("filePath:" + filePath + fileName);
101         string strbase64 = source.Substring(source.IndexOf(,) + 1);
102         strbase64 = strbase64.Trim(\0);
103         //Log.Debug("strbase64:" + strbase64);
104         byte[] arr = Convert.FromBase64String(strbase64);
105         using (MemoryStream ms = new MemoryStream(arr))
106         {
107             Log.Debug("进入了MemoryStream");
108             Bitmap bmp = new Bitmap(ms);
109             if (!Directory.Exists(Server.MapPath(filePath)))
110             {
111                 Log.Debug("没有Directory");
112                 Directory.CreateDirectory(Server.MapPath(filePath));
113             }
114             //if (!Directory.Exists(filePath))
115             //    Log.Debug("没有Directory");
116             //Directory.CreateDirectory(filePath);
117             //新建第二个bitmap类型的bmp2变量。
118             Bitmap bmp2 = new Bitmap(bmp, bmp.Width, bmp.Height);
119             //将第一个bmp拷贝到bmp2中
120             Graphics draw = Graphics.FromImage(bmp2);
121             draw.DrawImage(bmp, 0, 0);
122             draw.Dispose();
123             Log.Debug("保存图片前");
124             bmp2.Save(Server.MapPath(filePath + fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
125             Log.Debug("保存图片后");
126             //bmp.Save("test.bmp", ImageFormat.Bmp);
127             //bmp.Save("test.gif", ImageFormat.Gif);
128             //bmp.Save("test.png", ImageFormat.Png);
129             ms.Close();
130             return filePath + fileName;
131         }
132     }
133     catch (Exception ex)
134     {
135         return null;
136     }
137 }

 

C#保存Base64格式图片

标签:

原文地址:http://www.cnblogs.com/weishuanbao/p/5266189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!