ImageResize v2.0 for .NetCore, supports resize, crop, text and image watermark
ImageResize v2.0
Features :
- Resize, crop uploaded images on the server side,
- add textwatermark,
- add image watermark and change opacity
See samples here : http://demo.ziyad.info/en/ImageResize
Installation:
Install via nuget :
PM > Install-Package LazZiya.ImageResize -Version 2.0.0
Upload and resize an image
Handling uploaded files and resizing the images:
using System.Drawing;
foreach (var file in Request.Form.Files) {
if (file.Length > 0) {
using (var stream = file.OpenReadStream()) {
var uploadedImage = Image.FromStream(stream);
//returns Image file
var img = ImageResize.Scale(uploadedImage, 800, 600);
img.SaveAs($"wwwroot\\images\\{file.FileName}");
}
}
}
Image resize methods:
ImageResize supports below resizing methods :
Scale :
Auto scales image by width or height, and keeps aspect ratio same as original image.
ImageResize.Scale(Image img, int newWidth, int newHeight);
Scale By Width :
Scales image by width, auto adjests new height according to aspect ratio.
ImageResize.ScaleByWidth(Image img, int newWidth);
Scale By Height :
Scales image by height, auto adjusts new width according to aspect ratio.
ImageResize.ScaleByHeight(Image img, int newHeight);
Scale And Crop :
Scales uploaded image to fit target new width or new height (which fits first), then crops out the rest of the image.
Target spot can be defined as additional parameter, default is center.
ImageResize.ScaleAndCrop(Image img, int newWidth, int newHeight, TargetSpot spot);
Crop :
Directly crop a specified area of the image, without scaling.
ImageResize.Crop(Image img, int newWidth, int newHeight, TargetSpot spot);
TargetSpot Options :
Specifies that target spot used for cropping or placing text and image watermarks.
public enum TargetSpot { TopLeft, TopMiddle, TopRight, MiddleLeft, Center, MiddleRight, BottomLeft, BottomMiddle, BottomRight }
Add text watermark to the uploaded image :
Static method to add text watermark to any target spot, below is the method with pre-defined default values.
Sample usage to add "http://ziyad.info" as a text water mark:
img.TextWatermark("Http://Ziyad.info", //text watermark
"#DDAA9955", //text color: hex8 value
"#55AA9955", //background color: hex8 value
"Calibri", //font family
24, //font size
TargetSpot.BottomLeft, //targt spot tp place the text
FontStyle.Italic, //font style
10); //margin size, distance from bottom border
img.SaveAs($"wwwroot\\images\\{file.FileName}");
Add image watermark :
Static method to add image watermark to any target spot, with ability to adjust image watermark opacity.
Sample usage to add an image as watermark to the uploaded image :
img.ImageWatermark("wwwroot\\images\\myimage.png", //local path to the image watermark
TargetSpot.BottomRight, //add the image watermark to the bottom right area of the uploaded image
10, //keep 10px margin from the borders
40); //adjust watermark opacity to be 40 (0 - 100)
img.SaveAs($"wwwroot\\images\\{file.FileName}");