Save high resolution (size:width:1800px, height:1080px) canvas image to server using asp.net
-
I have this code to save a canvas image to the web server. But it is not working when the image has more than 200*200 pixels and high resolution: it does not save. A small image works; but a larger image with high resolution does not work. <script type="text/Javascript"> function drawShapes() { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); var imageObj = new Image(); imageObj.src = "im.jpg"; imageObj.onload = function () { context.drawImage(imageObj, 0, 0, 1800, 1080); } } </script> <script type="text/javascript"> $(document).ready(function () { $("#TZ").click(function () { var image = document.getElementById("myCanvas").toDataURL("image/png"); image = image.replace('data:image/png;base64,', ''); $.ajax({ type: 'POST', url: 'Include/CS.aspx/UploadImage', data: '{ "imageData" : "' + image + '" }', contentType: 'application/json; charset=utf-8', dataType: 'json', /*success: function (msg) { alert('Image saved successfully !'); }*/ }); }); }); </script> <body onload="drawShapes()"> <form id="form1" runat="server"> <div> <canvas id="myCanvas" width="1800" height="1080"></canvas> <a id="TZ">Save</a> </div> </form> </body> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Web.Script.Services; using System.Web.Services; [ScriptService] public partial class Include_CS : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod()] public static void UploadImage(string imageData) { string path = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/"); string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png"; using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(imageData); bw.Write(data); bw.Close(); } } } }
-
Answer:
I'm not sure if this is your situation, but ASP.net has an upload limit of 4mb, maybe the file you are generating is bigger than that. One way to solve this is to change your web.config's httpRuntime tag: <httpRuntime maxRequestLength="4096" /> 4096 is the default value (4mb), try to increase it to 16mb(16384) for example just to see if it works. Another very useful option is to use a free upload library such as http://www.plupload.com/. http://stackoverflow.com/a/4369096/2506478
Farid Karami at Stack Overflow Visit the source
Related Q & A:
- How can I allow user to create posts in website using ASP.NET?Best solution by Programmers
- How to Undo previous action in asp.net?Best solution by forums.asp.net
- How to Search using jQuery ASP.Net?Best solution by Stack Overflow
- How to Play Audio files in ASP.NET?Best solution by Stack Overflow
- Is there some kind of API for MSN protocol (for using in C# asp.net application?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.