![](/img/trans.png)
[英]In ASP.net C# how do I pass an argument from a Javascript function to the onclick handler
[英]ASP.NET C# How do I pass canvas image to controller as Bitmap
我有以下javascript,以字符串的形式将我的View
上的canvas
图像发送到控制器。 但我想将字符串转换为Bitmap
格式。 我怎么能这样做?
document.getElementById('save').addEventListener('click', function () {
var image = document.getElementById("canvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: 'POST',
url: "Attendance/Decode",
data: '{ "imageData" : "' + image + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("Done, Picture Sent.");
}
});
});
控制器方法:
public ActionResult Decode(string imageData)
{
//Convert imageData to Bitmap
}
有没有其他方法将canvas
图像作为Bitmap
发送到控制器?
如果您正在接收URI或Base64,则可以使用BitmapImage()将其转换为位图。
乌里:
BitmapImage bitmapImage = new BitmapImage(url);
BitmapImage bitmapImage = new BitmapImage();
byte[] byteBuffer = Convert.FromBase64String(base64String);
MemoryStream memoryStream = new MemoryStream(byteBuffer);
memoryStream.Position = 0;
bitmapImage.SetSource(memoryStream);
memoryStream.Close();
memoryStream = null;
byteBuffer = null;
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.