Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to Generate png Image from Html 5 Canvas
    primarykey
    data
    text
    <p>I've drawn a gradient image with a Bezier Curve on a HTML 5 Canvas like this:</p> <pre><code>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(); } </code></pre> <p>Now I want to save this image on HDD, for this I have formed this request:</p> <pre><code>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'); } }); } </code></pre> <p>On this server side, I've written this code (ASP.NET C#):</p> <pre><code>[HttpPost] public ActionResult Export() { string img = Request.Params["img"]; ConvertThis(img); return Content("true"); } public void ConvertThis(string ImageText) { if (ImageText.Length &gt; 0) { ImageText = ImageText.Substring(ImageText.IndexOf(",") + 1); Byte[] bitmapData; bitmapData = Convert.FromBase64String(ImageText); System.IO.File.WriteAllBytes(@"c:\logs\random.png", bitmapData); } } </code></pre> <p>But the image generated on the server side is totally different from the image drawn on the Html5 Canvas <img src="https://i.stack.imgur.com/gbbcG.png" alt="Server Image">.</p> <p>What am I doing wrong here..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload