C#保存Base64格式图片

 .前端页面代码
/**
* 通过图片本地路径获取图片真实大小,并进行压缩
*/
function getLocalRealSize(path, callback) {
var img = new Image();
var tempimg = new Image();
img.src = path;
img.onload = function() {
var height = img.naturalHeight;
var width = img.naturalWidth;
img.width = img.naturalWidth;
img.height = img.naturalHeight;
//压缩图片
var canvas = compressImage(img);
tempimg = canvas.toDataURL('image/jpeg'); callback(tempimg);
}
};
/**
* 压缩图片
*/
function compressImage(image) {
var canvas = document.createElement("canvas");
var mwidth = image.width;
var mheight = image.height;
var scale = ;
var i = ;
while (true) {
if (mwidth >> i <= && mheight >> i <= ) {
scale = Math.pow(0.5, i);
break;
}
i += ;
}
canvas.width = mwidth * scale;
canvas.height = mheight * scale;
canvas.getContext("2d").drawImage(image, , , mwidth * scale, mheight * scale);
return canvas;
}
//主页面 车主首页 跳转
document.getElementById("driverindex").addEventListener("tap", function() {
//获取img对象
var OwerImg = document.getElementById("owner-photo-img");
var OwerCardImg = document.getElementById("owner-cardphoto-img");
var OwerLicenceImg = document.getElementById("owner-driver1-img");
var OwerDrivingLicenceImg = document.getElementById("owner-driver2-img");
var CarImg = document.getElementById("owner-car-img");
var AssuranceImg = document.getElementById("owner-sure-img"); var oArray=source.split("|");
var totalCount=imgCount+oArray.length-;
if(totalCount<){
mui.toast("请上传完整信息");
return false;
}
uploadImg();
});
});
// 上传文件
var uploadImg = function() {
var server = imgUrl+'/clientapi/driver/ApplayCertification';
//outSet("开始上传:")
var wt = plus.nativeUI.showWaiting();
var task = plus.uploader.createUpload(server, {
method: "POST",
blocksize: ,
priority:
},
function(t, status) { //上传完成
if (t.state == && status == ) {
wt.close();
//outLine("上传成功:" + t.responseText);
//上传成功,跳转到车主首页
mui.toast('上传成功');
mui.fire(mycarPage, "pageflowrefresh" );
plus.webview.currentWebview().close();
mui.redirect('myaccount-mycar', '../myaccount/mycar.html');
} else {
//outLine("上传失败:" + status);
wt.close();
mui.toast('上传失败');
}
}
);
task.addData("token", UserInfo.token());
task.addData("source", source);
task.start();
}
.C#后台代码
//将base64编码的字符串转为图片并保存
protected string SaveBase64Image(string source, string path, int userID)
{
try
{
var now = DateTime.Now;
string filePath = path + now.ToString("yyyy-MM-dd") + "/" + userID.ToString() + "/";
string fileName = now.ToString("yyyyMMddhhmmssffff") + ".jpg";
Log.Debug("filePath:" + filePath + fileName);
string strbase64 = source.Substring(source.IndexOf(',') + );
strbase64 = strbase64.Trim('\0');
//Log.Debug("strbase64:" + strbase64);
byte[] arr = Convert.FromBase64String(strbase64);
using (MemoryStream ms = new MemoryStream(arr))
{
Log.Debug("进入了MemoryStream");
Bitmap bmp = new Bitmap(ms);
if (!Directory.Exists(Server.MapPath(filePath)))
{
Log.Debug("没有Directory");
Directory.CreateDirectory(Server.MapPath(filePath));
}
//if (!Directory.Exists(filePath))
// Log.Debug("没有Directory");
//Directory.CreateDirectory(filePath);
//新建第二个bitmap类型的bmp2变量。
Bitmap bmp2 = new Bitmap(bmp, bmp.Width, bmp.Height);
//将第一个bmp拷贝到bmp2中
Graphics draw = Graphics.FromImage(bmp2);
draw.DrawImage(bmp, , );
draw.Dispose();
Log.Debug("保存图片前");
bmp2.Save(Server.MapPath(filePath + fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
Log.Debug("保存图片后");
//bmp.Save("test.bmp", ImageFormat.Bmp);
//bmp.Save("test.gif", ImageFormat.Gif);
//bmp.Save("test.png", ImageFormat.Png);
ms.Close();
return filePath + fileName;
}
}
catch (Exception ex)
{
return null;
}
}
上一篇:uniapp微信小程序保存base64格式图片的方法


下一篇:Linux分区的注意事项以及远程连接排错