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;
}
}
C#保存Base64格式图片的更多相关文章
- uniapp微信小程序保存base64格式图片的方法
uniapp保存base64格式图片的方法首先第一要先获取用户的权限 saveAlbum(){//获取权限保存相册 uni.getSetting({//获取用户的当前设置 success:(res)= ...
- laravel 存储base64格式图片
laravel 存储base64格式图片 一.总结 一句话总结: 用正则替换base64图片编码的编码头即可 存储图片的话,用laravel可以用Storage的put方法,原生php可以用file_ ...
- 微信小程序 base64格式图片的显示及保存
当我们拿到如下base64格式的图片(如下图)时, base64格式的图片数据: 如何显示 ? 使用image标签,src属性添加data:image/png;base64, (注意:若imgData ...
- C# WEB.API 接收并解析保存base64格式的图片
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- js获取base64格式图片预览上传并用php保存到本地服务器指定文件夹
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 保存BASE64编码图片
1.前端上传用户图片时,一些K数较小图片,头像图标等 .以bass64编码后的字符串传到服务器. 2.服务器接收并保留到本地. // 页面上点击保存 $.post('/imgupload/save', ...
- base64格式图片转换为FormData对象进行上传
原理:理由ArrayBuffer.Blob和FormData var base64String = /*base64图片串*/; //这里对base64串进行操作,去掉url头,并转换为byte va ...
- dedecms不能保存jpeg格式图片的解决方法
方法如下: 进入织梦的后台管理目录,默认是dede文件夹,找到/inc/inc_archives_functions.php文件. 在文件里查找gif|jpg|,我找到了4个, 在它们后面加jpeg的 ...
- 微信支付特约商户进件中base64格式图片上传
微信图片上传接口地址:https://api.mch.weixin.qq.com/v3/merchant/media/upload 1.上传方法 1 using HttpHandlerDemo; 2 ...
随机推荐
- JQuery Placeholder - Input提示信息
JQuery Placeholder Placeholder属性是HTML5为Input添加的,在Input上提供一个占位符,文字形式展示输入文字预期值的提示信息. 如: 需要使用:placehold ...
- ucenter 整合外部网站,实现登录等操作
最近公司网站要出手机版,由于与论坛要有互通,实现会员的共享,由于对discuz不熟悉,起先考虑的是直接读取discuz的用户表,然后进行判断等操作,发现过于复杂,而且安全也不好. 前一段时间进行过论坛 ...
- WCF数据通讯
Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NET Fra ...
- (LinkedList)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- html字符字体转换
- python 过滤出某后缀名文件
以从某文件夹过滤出py文件为例: 法1: import glob import os os.chdir(“./”) for file in glob.glob(“*.py”): print file ...
- mongodb转换List实体(去掉多余的字段) 批量添加
Gson gson = new Gson(); String str = "list集合json字符串"; JsonObject object = gson.fromJson(st ...
- ios delegate, block, NSNotification用法
ios中实现callback可以通过两种方法,委托和NSNotification 委托的话是一对一的关系,例如一个UIViewController里有一个tableView, 将该viewContro ...
- MS sql server 基础知识回顾(二)-表连接和子查询
五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...