无法从 Html 5 Canvas 生成 png 图像
我在 HTML 5 Canvas 上绘制了带有贝塞尔曲线的渐变图像,如下所示:
var canvas = $('#canvas1')[0];
if (canvas.getContext) {
var context = canvas.getContext('2d');
var gradient = context.createLinearGradient(0, 0, 0, 100);
gradient.addColorStop(0, 'rgba(193,193,193,1)');
gradient.addColorStop(.20, 'rgba(160,160,160,1)');
gradient.addColorStop(.5, 'rgba(102,102,102,1)');
gradient.addColorStop(.7, 'rgba(91,91,91,1)');
gradient.addColorStop(.97, 'rgba(160,160,160,1)');
gradient.addColorStop(1, 'rgba(193,193,193,1)');
context.fillStyle = gradient;
context.fillRect(0, 0, 500, 100);
var gradient1 = context.createLinearGradient(0, 0, 0, 100);
gradient1.addColorStop(0, '#a1a1a1');
gradient1.addColorStop(.5, '#717171');
gradient1.addColorStop(1, '#a1a1a1');
context.beginPath();
context.moveTo(0, 0);
context.bezierCurveTo(0, 0, 20, 50, 0, 100);
context.lineWidth = 1;
//context.strokeStyle = 'black'; // line color
//context.stroke();
context.fillStyle = gradient1;
context.fill();
context.closePath();
}
现在我想将此图像保存在 HDD 上,为此我已形成此请求:
var canvas = $('#canvas1')[0];
if (canvas.getContext) {
var context = canvas.getContext('2d');
$.ajax({
url: '/Html5/Export',
type: 'POST',
data: 'img=' + escape(canvas.toDataURL("image/png")),
success: function (data) {
alert('data exported');
}
});
}
在该服务器端,我编写了此代码(ASP .NET C#):
[HttpPost]
public ActionResult Export()
{
string img = Request.Params["img"];
ConvertThis(img);
return Content("true");
}
public void ConvertThis(string ImageText)
{
if (ImageText.Length > 0)
{
ImageText = ImageText.Substring(ImageText.IndexOf(",") + 1);
Byte[] bitmapData;
bitmapData = Convert.FromBase64String(ImageText);
System.IO.File.WriteAllBytes(@"c:\logs\random.png", bitmapData);
}
}
但服务器端生成的图像与 Html5 Canvas 上绘制的图像完全不同 。
我在这里做错了什么..
I've drawn a gradient image with a Bezier Curve on a HTML 5 Canvas like this:
var canvas = $('#canvas1')[0];
if (canvas.getContext) {
var context = canvas.getContext('2d');
var gradient = context.createLinearGradient(0, 0, 0, 100);
gradient.addColorStop(0, 'rgba(193,193,193,1)');
gradient.addColorStop(.20, 'rgba(160,160,160,1)');
gradient.addColorStop(.5, 'rgba(102,102,102,1)');
gradient.addColorStop(.7, 'rgba(91,91,91,1)');
gradient.addColorStop(.97, 'rgba(160,160,160,1)');
gradient.addColorStop(1, 'rgba(193,193,193,1)');
context.fillStyle = gradient;
context.fillRect(0, 0, 500, 100);
var gradient1 = context.createLinearGradient(0, 0, 0, 100);
gradient1.addColorStop(0, '#a1a1a1');
gradient1.addColorStop(.5, '#717171');
gradient1.addColorStop(1, '#a1a1a1');
context.beginPath();
context.moveTo(0, 0);
context.bezierCurveTo(0, 0, 20, 50, 0, 100);
context.lineWidth = 1;
//context.strokeStyle = 'black'; // line color
//context.stroke();
context.fillStyle = gradient1;
context.fill();
context.closePath();
}
Now I want to save this image on HDD, for this I have formed this request:
var canvas = $('#canvas1')[0];
if (canvas.getContext) {
var context = canvas.getContext('2d');
$.ajax({
url: '/Html5/Export',
type: 'POST',
data: 'img=' + escape(canvas.toDataURL("image/png")),
success: function (data) {
alert('data exported');
}
});
}
On this server side, I've written this code (ASP.NET C#):
[HttpPost]
public ActionResult Export()
{
string img = Request.Params["img"];
ConvertThis(img);
return Content("true");
}
public void ConvertThis(string ImageText)
{
if (ImageText.Length > 0)
{
ImageText = ImageText.Substring(ImageText.IndexOf(",") + 1);
Byte[] bitmapData;
bitmapData = Convert.FromBase64String(ImageText);
System.IO.File.WriteAllBytes(@"c:\logs\random.png", bitmapData);
}
}
But the image generated on the server side is totally different from the image drawn on the Html5 Canvas .
What am I doing wrong here..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
返回带有 data uri 前缀的 base64 编码字符串,因此看起来您对其进行了两次编码。从“canvas.toDataURL”返回的字符串将如下所示:
如果您只对 Base64 编码的图像感兴趣,请像这样提取它:
returns a base64 encoded string with a data uri prefix, so it looks like you are encoding it twice. The string returned from 'canvas.toDataURL' will look something like this:
If you are interested in only the base64 encoded image, extract it like this:
escape(canvas.toDataURL("image/png"))
我认为应该是encodeURIComponent而不是escape。
encodeURIComponent 使用 HTTP 协议,而 escape 仅编码非 ASCII 字符。如果您使用 HTTP 协议,请不要使用转义。
escape(canvas.toDataURL("image/png"))
I think it should be encodeURIComponent instead of escape.
encodeURIComponent works with HTTP protocol while escape just encode non-ASCII characters. Don't use escape if you're working with HTTP protocol.
http://www.worldwidewhat.net/2011/06/ send-canvas-content-to-server/
也阅读评论,可能必须删除 data:image... 就像 user375025 所说的那样。假设您不久前就解决了这个问题,并为其他人提供了帮助。
http://www.worldwidewhat.net/2011/06/send-canvas-content-to-server/
read the comments too, might have to srip off the data:image... like user375025 stated. Assuming you figured this out some time ago, putting for others.