-->

Unable to Generate png Image from Html 5 Canvas

2020-08-01 11:29发布

问题:

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

回答1:

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(',')),


回答2:

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.



回答3:

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.