我可以在服务器上保存图像文件,但图像保存为空白图像画布上绘制的图像没有保存,但如果我画矩形,圆形然后图像保存很好请帮助解决我的代码中的问题......
脚本代码:
$(document).ready(function () {
var myCanvas = document.getElementById("Canvas1");
var myContext = myCanvas.getContext("2d");
var myImg = new Image();
myImg.src = "images/cabinbg.jpg";
myImg.onload = function () { myContext.drawImage(myImg, 0, 0); };
var dataURL = document.getElementById("Canvas1").toDataURL("image/png");
$('#<%=btnAdd.ClientID %>').live('click', function () {
dataURL = dataURL.replace('data:image/png;base64,', '');
$.ajax({
type: "POST",
url: "LayoutMaster.aspx/GetImage",
data: "{ 'image':'" + dataURL + "'}",
contentType: 'application/json; charset=utf-8',
dataType: 'json'
}).done(function (respond) {
//alert(respond);
});
});
.cs 文件
[System.Web.Services.WebMethod()]
public static void GetImage(string image)
{
if (image != "")
{
try
{
FileStream fs;
byte[] photo = Convert.FromBase64String(image);
fs = new FileStream(HostingEnvironment.MapPath("~/images/abc.png"), FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter br = new BinaryWriter(fs);
br.Write(photo);
br.Flush();
br.Close();
fs.Close();
}
catch (Exception e)
{
e.Message.ToString();
}
}
}