Learn MVC Using Angular Image Crop

Jun 22, 2017AngularASP.NET MVC
Summarize with AI: Google AI Claude ChatGPT Perplexity Grok

AI links open with a title + excerpt (these tools can't fetch the page themselves) — use "Copy full article" to paste the complete text for a fuller summary.

Share:

Introduction

This article demonstrates how to use MVC using Angular Image Crop and Bootstrap Filestyle in Visual Studio 2017.

Angular Image Crop

When you upload the image in Angular JS, this image crop directive helps to reduce the image size by using different area cutters.

Features

  • Fix width/height in crop areaD.
  • Fix crop area square/circle.
  • Fix type of image format.
  • Fix image quality.

With the following steps, you can use Angular Image Crop in Angular JS in MVC.

  • Create an MVC Project.
  • Configure Angular Image Crop and Bootstrap Filestyle.
  • Work in Angular Image Crop.

Create MVC Project

Open the visual studio 2017: Angular Go to New Menu >Click New and Project. Now it will open the New Project Window. Angular You can select ASP.NET Web Application on Framework 4.5. Enter the name of the project in the “Solution Name” text box, then click OK. Angular One more window should appear. Select the MVC Template in this popup and click OK. Now start cropping your image.

Configure Angular Image Crop and Bootstrap Filestyle

You can download the plugin from:

Open the _Layout.cshtml and refer the .js file from the downloaded folder to this view page.

       

Link your Angular configurable file in this code.

 

 

Angular Module

You will need to include the module as a dependency of your application.

var uiroute = angular .module('uiroute', ['ui.router','ngImgCrop']);

Work in Angular Image Crop

This tree control will work when you use the < img-crop > directive as HTML attributes.

 

Using Bootstrap Filestyle directive:

HTML Code

 
 
 
 
      Select an image file  
 
 
   
 

Crop type:

 
     
 
 
   
 
 
 
 
 
 
 
     
 
 
 
 
 

CSS Code

Set the style in the image crop area design.

.imgcrop-area {   width: 100%;   height: 410px;   overflow: hidden;   background: #e6e9ee;   }     .imgcrop-preview {   position: relative;   width: 100%;   height: 200px;   margin: 0 auto;   background: #e6e9ee;   text-align: center;   }     .imgcrop-preview:after {   content: attr(data-text);   display: block;   position: absolute;   height: 50%;   text-align: center;   margin: auto;   top: 0;   left: 0;   bottom: 0;   right: 0;   z-index: 0;   color: #8394a9;   }     .imgcrop-preview > img {   position: relative;   z-index: 1;   max-width: 100%;   }

Angular Controller

Initiate the crop area as “circle.”

$scope.reset = function () {   $scope.myImage = '';   $scope.myCroppedImage = '';   $scope.imgcropType = 'circle';   };   $scope.imgcropTypes = function (mode)   {   $scope.imgcropType = mode;   }

Angular Directive

You can use Bootstrap Filestyle as an Angular directive. When you will select the image using filestyle, set the element to the directive.

.directive('filestyle', filestyle);     functionfilestyle () {     controller.$inject = ['$scope', '$element'];   return {   restrict: 'A',   controller: controller   };     function controller($scope, $element) {   var options = $element.data();   options.classInput = $element.data('classinput') || options.classInput;     $element.filestyle(options);   }   }

Angular Controller

Write the attribute ID:

var handleFileSelect = function (evt) {   var file = evt.currentTarget.files[0];   var reader = new FileReader();   reader.onload = function (evt) {   $scope.$apply(function ($scope) {   $scope.myImage = evt.target.result;   });   };   if (file)   reader.readAsDataURL(file);   };     angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);

Click the F5 button and run the application. Now it will appear in the browser and you can see the result.

Output 1

Angular If you click the upload button, it will open file selectable windows and select any image in there.

Output 2

Angular Once open, the image will be loaded in the image preview. The initial crop area will be represented as “Square.”

Conclusion

In this article, we have learned MVC using Angular Image Crop using Bootstrap Filestyle. If you have any queries, please tell me through the comments section**.** Your comments are very valuable. Happy Coding!

#AngularJS#ASP.NET MVC#Visual Studio

Comments

Be the first to comment.

Leave a comment

Never shown publicly.

Comments are reviewed before appearing publicly.