无法从 Html 5 Canvas 生成 png 图像

发布于 2024-10-14 20:33:40 字数 2407 浏览 1 评论 0原文

我在 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 上绘制的图像完全不同 Server Image

我在这里做错了什么..

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 Server Image.

What am I doing wrong here..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2024-10-21 20:33:40
canvas.toDataURL("image/png")

返回带有 data uri 前缀的 base64 编码字符串,因此看起来您对其进行了两次编码。从“canvas.toDataURL”返回的字符串将如下所示:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...

如果您只对 Base64 编码的图像感兴趣,请像这样提取它:

var base64Image = canvas.toDataURL("image/png");
...
data: 'img=' + base64Image.substr(base64Image.indexOf(',')),
canvas.toDataURL("image/png")

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:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...

If you are interested in only the base64 encoded image, extract it like this:

var base64Image = canvas.toDataURL("image/png");
...
data: 'img=' + base64Image.substr(base64Image.indexOf(',')),
隱形的亼 2024-10-21 20:33:40

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.

把回忆走一遍 2024-10-21 20:33:40

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文