ImageResize v1

Image resizing tool for ASP.Net Core 2.x with support to add text water mark

Newer version is available here : ImageResize v2.0

To continue with v1.0 read below:

Installation:

Install via nuget :

PM > Install-Package LazZiya.ImageResize -Version 1.0.0

Upload and resize an image
Handling uploaded files and resizing the images:
 

foreach (var file in Request.Form.Files) {
    if (file.Length > 0) {
        using (var stream = file.OpenReadStream()) {
            var img = ImageResize.Resize(stream, 800, 600, ResizeMethod.Contain, TargetSpot.Center);
            img.SaveAs($"wwwroot\\images\\{file.FileName}");
        }    
    }
}

 

Image resize methods:

ImageResize supports below resizing methods :

  • ResizeMethod.None : No resize, original size will be kept
  • ResizeMethod.Contain : The resized image aspect ratio will be kept same as the original image. The uploaded image will be scaled down till width & height both are within the new size rect. In this resizing method the resized image will not be cropped, so depending on the specified new image size the new width or height meight be lower than the specified values. 
  • ResizeMethod.Crop : The resized image aspect ratio will be the new defined size aspect ratio, if the original image has different aspect ratio than the new size, the image will be cropped whenever width or height are equal to the new width or height accordingly.
  • ResizeMethod.SpotCrop : The image will be cropped according to the specified values depending on the TargetSpot parameter without resizing the original image.  

 

TargetSpot Options :

TargetSpot defines the resized image area to be cropped. It will be considered when the ResizeMethod is Crop or SpotCrop. When the resized image width or height is larger than the specified values, a crop operation will be done and the resized image will be in one of these 9 spots, :

- TargetSpot.TopLeft

- TargetSpot.TopMiddle

- TargetSpot.TopRight

- TargetSpot.MiddleLeft

- TargetSpot.Center

- TargetSpot.MiddleRight

- TargetSpot.BottomLeft

- TargetSpot.BottomMiddle

- TargetSpot.BottimRight

 

Add text watermark to the uploaded image
Below code will draw a colored text with a transparent background in the bottom left corner of the uploaded image:

img.TextWatermark(text: "Watermark Text",
                  color: "#DDAA9955",
                  bgColor: "#55AA9955",
                  fontFamily: "Calibri",
                  fontSize: 24,
                  spot: TargetSpot.BottomLeft,
                  style: FontStyle.Italic,
                  stick: false);

img.SaveAs($"wwwroot\\images\\{file.FileName}");

 

color : Defines the text color in hex8 numbers, first two numbers contains the opacity values.

bgColor : The text background color, if we need a text with no background simple specify zero value for the opacity e.g. #00000000. The background bar height will always have the font size x 2, e.g. if we specify font size as 24, the background bar height will be 48px.

spot : defines the spot where the text will be placed (see TargetSpot for more details).

stick : true to stick the text background to the top or bottom border, false to keep a small space between top or bottom border and the text background.

fontFamily : Any installed font should work properly, none latin alphabets also supported e.g. Arabic.