ASP.Net Web Photo Editing Tool Using HTML 5

Web Photo Editing Tool

In my previous article “Web Painting Tool Using HTML 5 CANVAS in ASP.NET" I explained how to create a simple Web Painting tool using HTML5 and jQuery. This article explains how to create a simple web-based photo editing tool using HTML5, jQuery and ASP.NET.

This article shows the details of how to do the following.

Capture Image

  1. Capture an image from a web camera (I used Webcam JS, you can download the script file from this link).

    Webcam JS is a JavaScript library for capturing images from a web camera and saving them as JPEG/PNG or any data URI format.

    I used this library to capture images from the web camera and display the captured image in the HTML 5 canvas for editing the photo, adding a smiley, text and send the final edited image by email.
  2. Add Sticker: Add stickers, for example lSmiley's, flowers, and so on to our captured photo.

  3. Add Border: Add a border to the captured photo.

  4. Add Text: Add text to the captured photo.

  5. Add Filters: Add filters to the captured photo, such as adding contrast, changing it to a Black & White photo, invert the color of the photo or convert to an original captured photo.

  6. Save and Send to Email: The final edited photo can be saved to the application root folder and you can send the edited photo to an Email address.

 

   7. Post Canvas Photo to Facebook. The Final Edited photo can be posted to Facebook.

Prerequisites

Visual Studio 2013 or Visual Studio 2010.

In the source code Zip file you can find both Visual Studio 2010 and Visual Studio 2013 Solutions. You can use any solution depending on your Visual Studio version.

WEB Camera (to capture your photo to edit and send to email).

Using the code

The main purpose is to make the program very simple and easy to use. All the functions have been well commented in the project. I have attached my sample program to this article for more details. Here we will see the procedure to create a photo editing tool using a HTML5 canvas.

HTML5: HTML5 is the new version of HTML. HTML5 has cross-platform support. That means that HTML5 can work in a PC, Tablet and a Smartphone. HTML5 should be started with a DOCTYPE, for example:

  1. <!DOCTYPE html> <html> <body></body> </html>  

Som of the new features in HTML5 are CANVAS, AUDIO, VIDEO and so on.

CANVAS

CANVAS is the element for 2D drawings using JavaScript. The Canvas has methods such as drawing paths, rectangles, arcs, text and so on.

The Canvas element looks as in the following.

  1. <canvas id="canvas" width="400" height="400"></canvas>  

Reference for HTML5 canvas.

The Canvas is nothing but a container for creating graphics. To create 2D graphics we need to use JavaScript. We will see the details here in the code.

Step 1

Add References:
Add an entire JavaScript library to your project to use the webcam capture. For webcam capture we need webcam.js, webcam.min.js and webcam.swf. You can download these files from here

Add References

Step 2

To add the Webcam:
Add the following code to initialize the web camera in your browser and display the web camera image.

  1. <table >  
  2.    <tr>  
  3.        <td align="center">  
  4.            <div id="my_camera"></div>  
  5.          <!-- Configure a few settings and attach camera -->  
  6.     <script language="JavaScript">  
  7.         Webcam.set({  
  8.             width: 320,  
  9.             height: 240,  
  10.             image_format: 'jpeg',  
  11.             jpeg_quality: 90  
  12.         });  
  13.         Webcam.attach('#my_camera');  
  14.     </script>  
  15.       
  16.              </td>  
  17.     </tr>  
  18.     <tr>  
  19.        <td align="center">  
  20.            <input type=button value="Capture Photo" onClick="takePhoto()">  
  21.         </td>  
  22.      </tr>  
  23.  </table> 

To capture the image: In the Capture button client click event, call the method takePhoto(). In this method using the webcam.js snap method we will receive the image from the live webcam. I stored the obtained image to the global variable and called the init method to add this photo to the HTML5 Canvas tag.

  1. function takePhoto() {  
  2.    // take your photo and add the photo as canvas Background Image  
  3.    Webcam.snap(function (data_uri) {  
  4.       imageObj_BG.src = data_uri;  
  5.       init('BG''');  
  6.    });  

Captured Photo

Step 3

To add Webcam, create a Canvas ELEMENT and declare the global variables and initialize the Canvas in JavaScript. In the code I used comments to easily understand the declarations.

HTML Canvas Part

  1. SECTION style="border-style: solid; border-width: 2px; width: 600px;">  
  2.   
  3. <CANVAS HEIGHT="452" WIDTH="600px" ID="canvas"

Your browser is not supporting HTML5 Canvas. Upgrade the browser to view this program or check it with Chrome or Firefox.

  1. </CANVAS>  
  2.   
  3. </SECTION> 

JavaScript Declaration Part

First add all the JavaScript references and styles to your ASP.NET page as in the following:

  1. <meta http-equiv="x-ua-compatible" content="IE=9" />  
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  3. <script type="text/javascript" src="Scripts/jquery-1.10.2.min.js"></script>  
  4. <script type="text/javascript" src="webcam.js"></script>  
  5. <link href="Content/myStyle.css" rel="stylesheet" type="text/css" /> 

Declare all the variables necessary for photo editing. In each declaration I have added comments to explain its usage.

  1. <SCRIPT>  
  2.     //public Canvas object to use in all the functions.  
  3.   
  4.     //Main canvas declaration   
  5.     var canvas;  
  6.     var ctx;  
  7.   
  8. // canvas declaration photo filter add  
  9.  var canvasEffect;  
  10.     var ctxEffect;  
  11.   
  12. //Width and Height of the canvas  
  13.     var WIDTH = 600;  
  14.     var HEIGHT = 452;  
  15.     //    var dragok = false;  
  16.     //Global color variable which will be used to store the selected color name.  
  17.     var Colors = "";  
  18.     var newPaint = false;  
  19.     var DrawingTypes = "";  
  20.   
  21.     //Circle default radius size  
  22.     var radius = 30;  
  23.     var radius_New = 30;  
  24.     var stickerWidth = 40, stickerHeight = 40;  
  25.     // Rectangle array  
  26.     rect = {},  
  27.     //drag= false defult to test for the draging  
  28. drag = false;  
  29.   
  30.     // Array to store all the old Shapes drawing details  
  31.     var rectStartXArray = new Array();  
  32.     var rectStartYArray = new Array();  
  33.     var rectWArray = new Array();  
  34.     var rectHArray = new Array();  
  35.     var rectColor = new Array();  
  36.     var DrawType_ARR = new Array();  
  37.     var radius_ARR = new Array();  
  38.   
  39.     var Text_ARR = new Array();  
  40.   
  41.       
  42.     //to add the Image this will be used to add all the stickers like Smiley,Animels,Flowers and etc  
  43.     var ImageNames = new Array();  
  44.     var imageCount = 0;  
  45.     var imageObj = new Image();  
  46.     var imageObj_BG = new Image();  
  47. var newImagename = '';  
  48.   
  49. // For the Filters effects adde to photo like Contrast,Black&White and etc.  
  50.     var isEffectadded = 'NO';  
  51.     var EffectType = 'black';  
  52.     var DrawBorder = "No";  
  53.   
  54. //Clear the Canvas  
  55. function clear() {  
  56.         ctx.clearRect(0, 0, WIDTH, HEIGHT);  
  57.     } 

init() Method: init is important since for each button click this function will be called and pass the parameter for each function type. In this method I will create an object for the canvas and this canvas object will be used in all other functions. Here for example the DrawType will be DrawImage, DrawText, DrawBorder, Place Photo as BG and Filter Effects and the Imagename parameter will be used to pass each sticker image name and so on. In this init method I will create Mouse events such as Mousedown, Mousemove and MouseUp to add a sticker, move a sticker, resize a sticker and so on.

  1. //Initialize the Canvas and Mouse events for Canvas  
  2.     function init(DrawType, ImageName) {  
  3.         newPaint = true;  
  4.         canvas = document.getElementById("canvas");  
  5.         ctx = canvas.getContext("2d");  
  6.   
  7.         canvasEffect = document.getElementById("canvas");  
  8.         ctxEffect = canvasEffect.getContext("2d");  
  9.   
  10.         x = 5;  
  11.         y = 5;  
  12.         if (ImageName) {  
  13.   
  14.             ImageNames[imageCount] = ImageName;  
  15.   
  16.             imageCount = imageCount + 1;  
  17.         }  
  18.   
  19.         DrawingTypes = DrawType;  
  20.   
  21.   
  22.         if (DrawType = 'BG') {  
  23.   
  24.             ctx.drawImage(imageObj_BG, 1, 1, canvas.width - 1, canvas.height - 1);  
  25.   
  26.         }  
  27.   
  28.         if (DrawingTypes == 'Effects') {  
  29.             isEffectadded = 'YES';  
  30.             EffectType = ImageName;  
  31.             Effects();  
  32.         }  
  33.   
  34.   
  35.         radius = 30;  
  36.         radius_New = radius;  
  37.         canvas.addEventListener('mousedown', mouseDown, false);  
  38.         canvas.addEventListener('mouseup', mouseUp, false);  
  39.         canvas.addEventListener('mousemove', mouseMove, false);  
  40.   
  41.   
  42.   
  43.         return setInterval(draw, 10);  
  44.     } 

Add Captured Photo to canvas

For example, to add the captured photo to the canvas we call the takePhoto method.

  1. <input type=button value="Capture Photo" onClick="takePhoto()"

We have already seen that in takePhoto we store the captured photo to a global image variable.

  1. function takePhoto() {  
  2.    // take your photo and add the photo as canvas Background Image  
  3.    Webcam.snap(function (data_uri) {  
  4.       imageObj_BG.src = data_uri;  
  5.       init('BG''');  
  6.    });  

In this method I called init('BG', '') and in the init method I will check the DrawType = 'BG'. If it's true then I will draw the captured image to the canvas as in the following.

  1. if (DrawType = 'BG') {  
  2.   
  3.    ctx.drawImage(imageObj_BG, 1, 1, canvas.width - 1, canvas.height - 1);  
  4.   

Add border/text/Sticker to Captured Photo: In the Border Image click event I passed the DrawType as "Border" and in the mouse move event I will call the draw() method. This method depends on the DrawingTypes selected. I will add the features to the canvas tag, for example if Border is selected then I will draw the border for the canvas tag. If Images is selected then I will add the selected sticker image to the canvas tag.

*Sticker to Captured Photo

  1. <img src="images/rect.png"  onClick="init('Border','')" />  
  2. <img src="images/Font.png"  onClick="init('DrawText','')" />  
  3.   
  4. <img src="images/smily8.png" width="36" height="36" onClick="init('Images','images/smily8.png')"/>  
  5. <img src="images/smily9.png" width="36" height="36" onClick="init('Images','images/smily9.png')"/>  
  6. <img src="images/smily10.png" width="36" height="36"   
  7. onClick="init('Images','images/smily10.png')"/>  
  8.   
  9. //Darw all Shaps,Text and add images   
  10.     function draw() {  
  11.   
  12.         ctx.beginPath();  
  13.         Colors = document.getElementById("SelectColor").value;  
  14.         ctx.fillStyle = "#" + Colors;  
  15.         switch (DrawingTypes) {  
  16.             case "Border":  
  17.                 ctx.strokeStyle = "#" + Colors;  
  18.                 ctx.lineWidth = 10;  
  19.                 ctx.strokeRect(0, 0, canvas.width, canvas.height)  
  20.                 DrawBorder = "YES";  
  21.                 //     ctx.rect(canvas.width - 4, 0, canvas.width - 4, canvas.height);  
  22.                 break;  
  23.   
  24.             case "Images":  
  25.   
  26.                 imageObj.src = ImageNames[imageCount - 1];  
  27.                 ctx.drawImage(imageObj, rect.startX, rect.startY, rect.w, rect.h);  
  28.                 //  ctx.drawImage(imageObj, rect.startX, rect.startY, stickerWidth, stickerHeight);  
  29.                 break;  
  30.             case "DrawText":  
  31.                 ctx.font = '40pt Calibri';  
  32.                 ctx.fillText($('#txtInput').val(), drawx, drawy);  
  33.   
  34.                 break;  
  35.   
  36.         }  
  37.         ctx.fill();  
  38.         // ctx.stroke();  
  39.     } 

Add Filter Effects to Captured Photo: To add the filter effects to the captured photo I created an Effects function. This method depends on the user clicked effect. I will change the photo to either Black & White, Contrast, Invert or original Image.

  1. <input type=button value="Black&White"  onClick="init('Effects', 'black')"/>  
  2. <input type=button value="Contrast"  onClick="init('Effects', 'contrast')"/>  
  3. <input type=button value="Invert Color"  onClick="init('Effects', 'invertColors')"/>  
  4. <input type=button value="OriginalImage"  onClick="init('Effects', 'OriginalImage')"/>  
  5.   
  6.   
  7. //Add alll Effects which we need to change for image  
  8.     function Effects() {  
  9.   
  10.         if (isEffectadded == 'YES') {  
  11.             var imgd = ctxEffect.getImageData(0, 0, canvas.width, canvas.height);  
  12.             var pix = imgd.data;  
  13.             switch (EffectType) {  
  14.                 case "black":  
  15.                     for (var i = 0, n = pix.length; i < n; i += 4) {  
  16.                       var grayscale = pix[i] * .3 + pix[i + 1] * .59 + pix[i + 2] * .11;  
  17.                         pix[i] = grayscale;   // red            
  18.                         pix[i + 1] = grayscale;   // green            
  19.                         pix[i + 2] = grayscale;   // blue      
  20.   
  21.                         // alpha         
  22.                     }  
  23.   
  24.                     ctxEffect.putImageData(imgd, 0, 0);  
  25.   
  26.                     break;  
  27.   
  28.                 case "contrast":  
  29.                     var contrast = 40;  
  30.                     var factor = (259 * (contrast + 255)) / (255 * (259 - contrast));  
  31.                     for (var i = 0; i < pix.length; i += 4) {  
  32.   
  33.                         pix[i] = factor * (pix[i] - 128) + 128;  
  34.                         pix[i + 1] = factor * (pix[i + 1] - 128) + 128;  
  35.                         pix[i + 2] = factor * (pix[i + 2] - 128) + 128;  
  36.                     }  
  37.                     // overwrite original image  
  38.                     ctxEffect.putImageData(imgd, 0, 0);  
  39.                     break;  
  40.   
  41.                 case "invertColors":  
  42.                     for (var i = 0; i < pix.length; i += 4) {  
  43.                         // red  
  44.                         pix[i] = 255 - pix[i];  
  45.                         // green  
  46.                         pix[i + 1] = 255 - pix[i + 1];  
  47.                         // blue  
  48.                         pix[i + 2] = 255 - pix[i + 2];  
  49.                     }  
  50.                     // overwrite original image  
  51.                     ctxEffect.putImageData(imgd, 0, 0);  
  52.                     break;  
  53.   
  54.                 case "OriginalImage":  
  55.                     for (var i = 0; i < pix.length; i += 4) {  
  56.                         // red  
  57.                         pix[i] = pix[i];  
  58.                         // green  
  59.                         pix[i + 1] = pix[i + 1];  
  60.                         // blue  
  61.                         pix[i + 2] = pix[i + 2];  
  62.                     }  
  63.                     // overwrite original image  
  64.                     ctxEffect.putImageData(imgd, 0, 0);  
  65.                     break;  
  66.             }  
  67.         }  
  68.     } 

Here we can see each filter photo output. The first we have is a Black & White photo. Change the captured photo to Black & White.

filter Photo output

Next we have Invert Photo. Invert the captured photo as in the following.

Invert Photo

Next we will add Contrast to captured photo as invert like in the following.

add Contrast to captured photo

Finally we have the original photo that was captured from the web camera initially.

original Photo

Save and Send Email

In the send email button client click, I will store the Canvas image to the hidden field.

store the Canvas Image

  1.   <asp:Button ID="btnImage" runat="server" Text="Send Email"   
  2.              OnClientClick = "sendEmail();return true;" onclick="btnImage_Click" />  
  3.   
  4.   
  5.  function sendEmail() {  
  6.     var m = confirm("Are you sure to Save ");  
  7.     if (m) {  
  8.   
  9.         var image_NEW = document.getElementById("canvas").toDataURL("image/png");  
  10.         image_NEW = image_NEW.replace('data:image/png;base64,''');  
  11.         $("#<%=hidImage.ClientID%>").val(image_NEW);  
  12.         alert('Image saved to your root Folder and email send !');  
  13.     }  
  14.   

In the code behind button click event I will get the hidden field value and store the final result image to the application root folder. This image will be used to send an email.

  1. protected void btnImage_Click(object sender, EventArgs e)  
  2. {  
  3.   
  4.     string imageData = this.hidImage.Value;  
  5.   
  6.     Random rnd = new Random();  
  7.     string imagePath = HttpContext.Current.Server.MapPath("Shanuimg" + rnd.Next(12, 2000).ToString() + ".jpg");  
  8.   
  9.     using (FileStream fs = new FileStream(imagePath, FileMode.Create))  
  10.     {  
  11.         using (BinaryWriter bw = new BinaryWriter(fs))  
  12.         {  
  13.             byte[] data = Convert.FromBase64String(imageData);  
  14.             bw.Write(data);  
  15.             bw.Close();  
  16.   
  17.             sendMail(imagePath);  
  18.         }  
  19.     }  

In the button click event after the image is saved to the root folder I will send the path to the sendMail method.

In this method using the user entered From and To email address I will send the photo with the subject and message to the email.

  1. private void sendMail(string FilePath)  
  2. {  
  3.     MailMessage message = new MailMessage();  
  4.     SmtpClient smtpClient = new SmtpClient();  
  5.     string msg = string.Empty;  
  6.     try  
  7.     {  
  8.         MailAddress fromAddress = new MailAddress(txtFromEmail.Text.Trim());  
  9.         message.From = fromAddress;  
  10.         message.To.Add(txtToEmail.Text.Trim());  
  11.   
  12.         message.Attachments.Add(new Attachment(FilePath));  
  13.   
  14.         message.Subject = txtSub.Text.Trim();  
  15.         message.IsBodyHtml = true;  
  16.         message.Body = txtMessage.Text.Trim();  
  17.         smtpClient.Host = "smtp.gmail.com";  
  18.         smtpClient.Port = 587;  
  19.         smtpClient.EnableSsl = true;  
  20.         smtpClient.UseDefaultCredentials = true;  
  21.         smtpClient.Credentials = new System.Net.NetworkCredential(userGmailEmailID, userGmailPasswod);  
  22.   
  23.         smtpClient.Send(message);  
  24.         msg = "Successful<BR>";  
  25.     }  
  26.     catch (Exception ex)  
  27.     {  
  28.         msg = ex.Message;  
  29.     }  
  30.   
  31.   
  32.   

Note: Here I used the host as smtp.gmail.com and in System.Net.NetworkCredential(userGmailEmailID, userGmailPasswod); you need to provide your Gmail Email address and Gmail password to send the email.

I have declared the variable as global as in the following so that the user can add their own Gmail Email address and Gmail password.

  1. String userGmailEmailID = "YourGamilEmailAddress";  
  2. string userGmailPasswod = "YourGmailPassword"
photo editing tool

Post Photo to Facebook

 

To post our photo tofacebook we need to a Facebook APPID.To create our APPID go to https://developers.facebook.com/ and login using your facebook id.

After Login to create New App ID enter yourdisplay name and click Create App ID
 
Now you can see your App ID has been created.You can use this App ID to post your image to Facebook.
 
 

Click on Settings and add your website URL in case, if you’re developing as localhost in site URL you can give the localhost URL as below.

 

 

Click Settings - > Advanced and set theEmbedded browser OAutho Login to “YES”
 
 
Send to FB: Using Facebook API we can pass the Canvas converted base64 Image to Facebook using our App ID.Here is the reference link which explains how to convert and embed HTML5 Canvas 5 Image to base64 . https://github.com/DanBrown180/html5-canvas-post-to-facebook-base64 
 
  1. INPUT TYPE ="Button" id="btnFB" VALUE=" Send to FB " onClick="sendtoFB()">  
  2.   
  3.   
  4. function sendtoFB() {  
  5.         var m = confirm("Are you sure Post in FaceBook ");  
  6.         if (m) {  
  7.   
  8.   
  9.             $.getScript('//connect.facebook.net/en_US/all.js'function () {  
  10.                 // Load the APP / SDK  
  11.                 FB.init({  
  12.                     appId: '398343823690176'// App ID from the App Dashboard  
  13.                     cookie: true// set sessions cookies to allow your server to access the session?  
  14.                     xfbml: true// parse XFBML tags on this page?  
  15.                     frictionlessRequests: true,  
  16.                     oauth: true  
  17.                 });  
  18.                 FB.login(function (response) {  
  19.                     if (response.authResponse) {  
  20.                         window.authToken = response.authResponse.accessToken;  
  21.                         PostImageToFacebook(window.authToken)  
  22.                     } else {  
  23.                     }  
  24.                 }, {  
  25.                     scope: 'publish_actions'  
  26.                 });  
  27.             });  
  28.   
  29.         }  
  30.   
  31.     }  

When user clicks on Send to FB button they can login to theirFacebook for posting the canvas Photo.


Once the photo has been posted we can see our new photo in our Facebook page.
 
Tested browsers:
  • Chrome
  • Firefox
  • IE  10

Up Next
    Ebook Download
    View all
    Learn
    View all