gpt4 book ai didi

javascript - 使用 HTML5 Canvas 的 ASP.NET 绘图应用程序。保存到服务器。 JavaScript 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:06:02 26 4
gpt4 key购买 nike

我使用 HTML5 Canvas 构建了一个简单的 ASP.NET 绘图应用程序。我还编写了一些代码,将 Canvas 的 PNG 图像上传到服务器。我的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CanvasToServer._Default" %>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>iScribble Canvas To Server</title>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>

<!--Load Draw application code-->
<script type="text/javascript" src="JScript.js"></script>

<!--CSS-->
<style type="text/css">
html, body
{
font-family: "Helvetica Neue" , Helvetica, Arial, sans-serif;
}
#graph
{
border: 1px solid #000;
}
#controls
{
font-family: "Helvetica Neue" , Helvetica, Arial, sans-serif;
font-weight: bold;
font-size: smaller;
padding: 3px;
width: 594px;
height: 25px;
}
select
{
font-family: "Helvetica Neue" , Helvetica, Arial, sans-serif;
font-size: medium;
}
</style>

<script type="text/javascript">
// Send the canvas image to the server.
$(function () {
$("#btnSend").click(function () {
var image = graph[0].toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');

$.ajax({
type: 'POST',
url: 'Default.aspx/UploadImage',
data: '{ "imageData" : "' + image + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert('Image sent!');
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">

<div id="controls">
Size:
<select id="thickness" class="fixed">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
Color:
<select id="color">
<option value="#FFFFFF">#FFFFFF</option>
<option value="#AAAAAA">#AAAAAA</option>
<option value="#666666">#666666</option>
<option value="#000000">#000000</option>
<option value="#9BA16C">#9BA16C</option>
<option value="#CC8F2B">#CC8F2B</option>
<option value="#63631D">#63631D</option>
</select>
</div>

<canvas id="graph" width="200" height="150"></canvas>

<input id="btnSend" type="button" value="Send To Server" />

</form>
</body>
</html>

后面的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.IO;
using System.Web.Script.Services;

namespace CanvasToServer
{
[ScriptService]
public partial class _Default : System.Web.UI.Page
{
[WebMethod()]
public static void UploadImage(string imageData)
{
FileStream fs = new FileStream("C:\\image.png", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);

byte[] data = Convert.FromBase64String(imageData);

bw.Write(data);
bw.Close();
}
}
}

应用程序的绘图部分工作正常。

当我单击“发送到服务器”按钮时出现问题,我收到 JavaScript 错误:

'图未定义'

在线:

var image = graph[0].toDataURL("image/png");

谁能帮我解决这个问题。

提前致谢。

沃尔特

最佳答案

替换行:

var image = graph[0].toDataURL("image/png");

与:

var image = document.getElementById("graph").toDataURL("image/png");

成功了。

感谢 vytautas.ziurlis http://www.zvytas.com/

关于javascript - 使用 HTML5 Canvas 的 ASP.NET 绘图应用程序。保存到服务器。 JavaScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7993588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com