繁体   English   中英

ASP.NET C#如何将画布图像作为Bitmap传递给控制器

[英]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);

Base64String:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM