-
İçerikler özgün değildir. Sadece sık sık lazım olan işime yarayacak olan notları almış olduğum kişisel bloğumdur.
26 Ocak 2014 Pazar
Draw a Canvas image, Upload and save it on the server
Kaynak:http://www.codicode.com/art/upload_and_save_a_canvas_image_to_the_server.aspx
In this tutorial I will draw a picture on a Html 5 Canvas element and send it to the server to be stored.
When the user presses the “Draw Picture” button, a gradient and some text will be drawn to onto the canvas element and when the user presses "Send image to the server" button, the script extracts the image data from the Canvas and upload it to your server. The server side code in this tutorial is written with Asp.Net C#.
First we create a Html page containing 3 elements :
1 - The canvas element: we will actually draw things onto the canvas using javascript.
2 - The "Draw picture" button.
3 - "Save canvas to server" button : this action will send the Image data to the server to be saved as a file.
In this tutorial I will draw a picture on a Html 5 Canvas element and send it to the server to be stored.
When the user presses the “Draw Picture” button, a gradient and some text will be drawn to onto the canvas element and when the user presses "Send image to the server" button, the script extracts the image data from the Canvas and upload it to your server. The server side code in this tutorial is written with Asp.Net C#.
First we create a Html page containing 3 elements :
1 - The canvas element: we will actually draw things onto the canvas using javascript.
2 - The "Draw picture" button.
3 - "Save canvas to server" button : this action will send the Image data to the server to be saved as a file.
<html> <head> </head> <body> <form id="form1" runat="server"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> <script type="text/javascript" src="JsCode.js"></script> <div align="center"> <canvas id="myCanvas" width="300" height="200"></canvas> <br /><br /> <table><tr><td><button onclick="javascript:DrawPic();return false;">Draw Picture</button></td><td><button onclick="javascript:UploadPic();return false;">Upload Picture to Server</button></td></tr></table> </div> </form> </body> </html>Then we write the 2 javascript functions DrawPic() and UploadPic() inside a javascript file "JsCode.js" (referenced earlier in the html code) :
function DrawPic() { // Get the canvas element and its 2d context var Cnv = document.getElementById('myCanvas'); var Cntx = Cnv.getContext('2d'); // Create gradient var Grd = Cntx.createRadialGradient(100, 100, 20, 140, 100, 230); Grd.addColorStop(0, "red"); Grd.addColorStop(1, "black"); // Fill with gradient Cntx.fillStyle = Grd; Cntx.fillRect(0, 0, 300, 200); // Write some text for (i=1; i<10 ; i++) { Cntx.fillStyle = "white"; Cntx.font = "36px Verdana"; Cntx.globalAlpha = (i-1) / 9; Cntx.fillText("Codicode.com", i * 3 , i * 20); } } function UploadPic() { // Generate the image data var Pic = document.getElementById("myCanvas").toDataURL("image/png"); Pic = Pic.replace(/^data:image\/(png|jpg);base64,/, "") // Sending the image data to Server $.ajax({ type: 'POST', url: 'Save_Picture.aspx/UploadPic', data: '{ "imageData" : "' + Pic + '" }', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (msg) { alert("Done, Picture Uploaded."); } }); }And finally our C# server-side code snippet, we create a blank aspx page with the following code behind to read and save the picture :
using System; using System.Web; using System.IO; using System.Web.Script.Services; using System.Web.Services; [ScriptService] public partial class Save_Picture : System.Web.UI.Page { [WebMethod()] public static void UploadPic (string imageData) { string Pic_Path = HttpContext.Current.Server.MapPath("MyPicture.png"); using (FileStream fs = new FileStream(Pic_Path, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(imageData); bw.Write(data); bw.Close(); } } } }A sample website to run and test this code is available in the attached file, Thanks.
|
Kaydol:
Kayıt Yorumları
(
Atom
)
search this website
email updates
Like us on facebook
Katkıda bulunanlar
Blogger tarafından desteklenmektedir.
Etiketler
- AJAX (3)
- alertbox (1)
- android (1)
- Asp.net (22)
- blogger (1)
- bootstrap (2)
- C# (1)
- Canvas (3)
- capture (2)
- datetime (1)
- dizüstü (1)
- dizüstü bilgisayardan interneti paylaştırmak (1)
- editör yapma (1)
- facebook api (1)
- Flash (1)
- format (1)
- geometri (6)
- google (1)
- Graf (1)
- Graf Teorisi (1)
- Hatalar (4)
- Html (1)
- html5 (2)
- iframe (1)
- ilköğretim (4)
- javascript (2)
- jquery (7)
- JSON (2)
- kablosuz (1)
- Karakter (1)
- Kodlama örnekleri (2)
- kütle (4)
- Laravel (2)
- laravel g-zip (1)
- Listbox (1)
- Matematik (20)
- matematikistan (2)
- matematikistan.net (16)
- mongoDB (1)
- msdos (1)
- mysql (4)
- netbeans (1)
- phonegap (1)
- php (3)
- php ile benzer tags (1)
- php similar tags (1)
- routing (2)
- Samsung (1)
- Save (1)
- Save Canvas (2)
- SOAP (1)
- tags (1)
- tarih (1)
- Thumbnail (1)
- Türkçe (1)
- türkçe karakter (1)
- Türksat 4A (1)
- url routing (2)
- utf-8 (1)
- Validation Kontrolleri (3)
- wifi (1)
- WYSIWYG (1)
- WYSIWYG editör (1)
- xml (1)
İşe Yarar Siteler
Blog Archive
-
2014
(91)
- Aralık (1)
- Kasım (4)
- Ekim (9)
- Eylül (5)
- Ağustos (3)
- Nisan (1)
- Mart (20)
- Şubat (28)
-
Ocak
(20)
- İçerikten Html Kodu temizleme
- Asp.Net MVC Model’den gelen Byte Array Resmi Görün...
- Türkçe Karakter Sorunu
- Flash (FLA) Dosyalarını HTML5 Formatına Dönüştürme
- JavaScript ve CSS Dosyalarının Boyutunu Online Sık...
- Online C# compiler
- C# Web Sitesinden Veri Çekmek (HmtlAgilityPack Kul...
- Draw a Canvas image, Upload and save it on the server
- WYSIWYG Editör Yapımı
- Dizüstü bilgisayarı kablosuz bir modem olarak kull...
- Asp.net User controldeki(.cs) değişkeni sayfada ya...
- Master Page image sorunu
- C# Repeater Sayfalama Datalist Sayfalama
- Url Routing Hosta atınca çalışmama sorunu
- Asp.Net Routing-Klasör Şeklinde Sayfalar
- İlköğretim Matematik Dersinde Çoklu Zeka Kuramının...
- Fraktal ve Fraktal Geometri Nedir
- Bilgisayar Donanımlı Ortamda Fonksiyon ve Graf
- İlköğretim Sınıflarında Kütle Ağırlık Kavramının K...
- Asp.Net Validation of viewstate MAC failed Hatası
Popular Posts
-
Facebook uygulama geliştirirken bu tarz bir hata aldım. Çözümü : 1) https://developers.facebook.com sayfasından uygulamanın “Settings” ...
-
How to delete, disable and exit Demo Live Unit Retail Mode on the Samsung Galaxy S5 1. The first thing to do is to disable the retail mod...
-
Kaynak:http://www.codicode.com/art/upload_and_save_a_canvas_image_to_the_server.aspx In this tutorial I will draw a picture on a Html 5 C...
-
http://www.codeproject.com/Articles/9797/Pager-Control-for-Repeater-DataList-or-an-Enumerab SP.Net projelerinde çokça kullandığımız Repea...
-
başlat>çalıştır>regedit burdan aşağıdaki değerleri silin; HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug\De...
-
Gv-21 T21 android Tv Box Rom İşletim Sistem Kurulumu T301_120_20130111 Kalsörü açtıktan sonra NS115-Burn Tıklayıp çıkan yonerg el...
-
Öncelikle http://htmlagilitypack.codeplex.com/ sitesinden Html Agility Pack i indiriyoruz sonra projemizde sağ üstdeki solution ex...
-
Kaynak:http://bcakir.com/jquery-seciciler-ve-filtreler.html Kullanımı Açıklaması $(“p”) bütün paragrafları seçer $(“div p”) ...
-
Kaynak: http://www.buradayaziyor.com/index.php/2012/07/bu-site-bilgisayariniza-zarar-verebilir/ Burada yanlış giden bir şeyler var We...
-
Kaynak:http://www.furkanozden.net/2013/01/html5-progress-bar.html Bazı makaleleri yazarken resme ihtiyaç duyulur. Eğer makalede s...
© Arif Erzin Bloğu-Kendime Notlar 2013 . Powered by Bootstrap , Blogger templates and RWD Testing Tool
Hiç yorum yok :
Yorum Gönder