Manipulating Images using Image Processor in Asp.Net

TGH Word Cloud
TGH Word Cloud

Estimated reading time: 6 minutes

Since asp.net web development team understands that working with images; you don’t always need web control. There are better options to this and in this post, they will talk about these options. Read this post to know more.

Introduction

For working with images in Asp.net, it is not always required to make use of web control. If multiple files are being used and these are not required to be displayed or it is required to display a thumbnail to the browser, then prefer the use of the Graphics core class library instead of using web control. Another option for working with images is using ImageProcessor.

An Image processor is a group of libraries that are written in c#.These are lightweight libraries and helps programmers in manipulating images on the fly using Asp.Net 4.5

There are two important libraries in the Imageprocessor

  1. Imageprocess: These are used in both web applications as well as desktop applications.
  2. Web: An extension of image processing for Asp.Net.

It is open source, extensible, light weight and easy to use.

Manipulating Images using Image Processor in Asp.Net 1

History

Microsoft announced in 2014 that they are building a cross-platform and open source framework that will have a subset of entire .Net framework. Everyone was excited about it except few people who were worried for no discussion on graphics API. Many issues were reported on the Github for the same and only few people got involved in this.

Developers used System.Drawing to perform common tasks. However, it made the Code slow and leads to memory leaks.As an extension to this idea, Image Processor core is used.

The Elevator Pitch

The common tasks of imaging should be accessed by all the developers. Image resize and cropping should be easily performed with a simple API. Programmers need not to worry for memory leaks or about binding with native libraries or performing deployment in multiple environments. It should be cross-platform and stable.

The Imageprocessor is designed keeping all this in mind.

The imageprocessor core contains the following:

  1. Algorithms for basic operations such as flip, resize, rotate, crop, edge direction etc.
  2. Completely supports four codecs: png, bmp, gif and jpeg.
  3. Complete writing and reading capability with a simple API.
  4. An Image class that is generic and compatible with mono game and game studio of Microsoft XNA.
  5. A new snazzy logo.

Example:

  1. The following code is toresize an image with the help of bicubic resampler.

using (FileStreaminputFs = File.OpenRead(“test1.jpg”))
using (FileStreamoutputFs = File.OpenWrite(“test2.jpg”))
{
ImageimageFs = newImage (inputFs);
imageFs.Resize (imageFs.Width / 2, imageFs.Height / 2).Save(outputFs);
}

  1. The below code is used to command the image for resizing using gamma correction.

using (FileStreaminputFs = File.OpenRead(“test1.jpg”))
using (FileStreamoutputFs = File.OpenWrite(“test2.jpg”))
{
ImageimageFs = newImage (inputFs);
imageFs.Resize (imageFs.Width / 2, imageFs.Height / 2,true).Save (outputFs);
}

The code shown above is used for resizing an image with specific dimensions. The properties are passed to function in this case. The object of WebImage allows easy cropping, resizing, and rotation of image. Also it is possible to easily insert watermark to image using it. Other options like System.Drawing.Image can be performed on back-end in case it is converted to native assemblies of .Net.

In case if it is required by Client to add some client-side functionality such as client should be able to select specific areas for cropping with some dragging tools, it can be implemented by using jQuery libraries. The same can be added to the Asp.Net application with the help of script tag, either on the entire website or on the specific page where it is needed. The application performance can also be increased by adding JavaScript libraries.

  1. The following code is used to set the pixel values:

       ImageimageFs = newImage(300,300);
using (PixelAccessor<Color, uint>pixelsFs = imageFs.Lock())
{
pixelsFs[300, 300] = Color.Green;
}

Image class is implementation of the generic class defined by Image<TColor,TPacked>.

TColor is used to implement an interface that is similar to the MonoGame and Microsoft game studio. It allows proper functioning of library with frameworks helps in controlling the usage of memory.

Image processing using Aspose API

Aspose is an advanced API for image processing in Asp.net. It allows programmers to draw, edit, create and convert images in .Net applications. It is independent to other applications. Aspose.imaging makes it possible for developers to save their images in the format of Photoshop without any need to install specific image editor on the system. It is powerful, flexible and stable. Similar to the other APIs file format, Aspose.imaging provides support for all image formats and functions for processing images in Silverlight and Asp.net.

Main Features of Image processing APIs

  1. All images are created from scratch.
  2. It is possible to edit and load the existing images.
  3. Images can be exported to different formats.
  4. It is possible to resize rotate, flip and crop images.
  5. It is possible to add watermarks to the images.
  6. Frames can be extracted from multipage image.
  7. The images are drawn with basic functionalities such as manipulation of pixel information.
  8. All images are drawn with advanced features such as Graphics Path and Graphics.
  9. It is possible to draw rectangles, lines, curves and other different shapes.
  10. It is possible to fill textures and gradients in closed shapes.
  11. It is possible to insert text with fine control options over the font.
  12. Images can be drawn by loading prevailing data of images.

Drawing Features

Many advanced features of drawing are supported by Aspose. Imaging. Image surface can be created by developers by either using Paths or Graphics like functionality or by using the bitmap information.

Convert Images to Other Formats

Many advanced algorithms are provided by Apose.Imaging that is useful in converting images to other formats. These are easy to use and specialized classes are used their encapsulation. They can convert images to Jpeg, GIF, WMF, PSD, TIFF, BMP and many more formats for better performance and reliability.

Register File Formats for Custom Image

Another feature provided by Apose.Imaging is registering of custom formats for images. It allows programmers to register custom formats for creating, exporting or opening images with the help of /Net libraries.

This post explains the image processor use for manipulating images in asp.net. If you need to ask anything related to the subject from the asp.net web development team, you can make comments below.

Share this content:

Click to rate this post!
[Total: 0 Average: 0]
Avatar for Andrew Armstrong

About Andrew Armstrong

Founder of TechyGeeksHome and Head Editor for over 15 years! IT expert in multiple areas for over 26 years. Sharing experience and knowledge whenever possible! Making IT Happen.

View all posts by Andrew Armstrong

One Comment on “Manipulating Images using Image Processor in Asp.Net”

Leave a Reply

Your email address will not be published. Required fields are marked *