Image resizing tool for .Net applications, with support to add text/image watermark. This package is built on .NetStandard 2.0 so it supports wide range of compatible platforms (e.g. Asp.Net Core etc).
Install via nuget (enable Include prerelease to check for latest test versions):
Install-Package LazZiya.ImageResize
using System.Drawing;
using LazZiya.ImageResize;
using(var img = Image.FromFile(@"wwwroot\images\image-file.jpg"))
{
img.ScaleByWidth(600)
.SaveAs(@"wwwroot\images\resized-image.jpg");
}
AddTextWatermark
method accepts argument of type TextWaterMarkOptions
that allows to customize the text.
To change opacity of the text/outline/background just use the relevant Color
with specified alpha value (0 - 255), 0 full opacity, 255 full color.
using(var img = Image.FromFile(@"wwwroot\images\image-file.jpg"))
{
var tOps = new TextWatermarkOptions
{
// Change text color and opacity
// Text opacity range depends on Color's alpha channel (0 - 255)
TextColor = Color.FromArgb(50, Color.White),
// Add text outline
// Outline color opacity range depends on Color's alpha channel (0 - 255)
OutlineColor = Color.FromArgb(255, Color.Black)
};
img.AddTextWatermark("http://ziyad.info", tOps)
.SaveAs(@"wwwroot\images\new-image.jpg");
}
AddImageWatermark
method accepts argument of type ImageWatermarkOptions
that allows to specify watermark position etc.
using(var img = Image.FromFile(@"wwwroot\images\image-file.jpg"))
{
var iOps = new ImageWatermarkOptions
{
// Change image opacity (0 - 100)
Opacity = 50,
// Change image watermark location
Location = TargetSpot.BottomRight
};
img.AddImageWatermark(@"wwwroot\images\logo.png", iOps)
.SaveAs(@"wwwroot\images\new-image.jpg");
}
All ImageResize methods can be chained together to provide easy image processing. Below sample shows how to handle uploaded files, resize them, add image and text watermarks:
using System.Drawing;
using LazZiya.ImageResize;
foreach (var file in Request.Form.Files)
{
if (file.Length > 0)
{
using (var stream = file.OpenReadStream())
{
using(var img = Image.FromStream(stream))
{
img.ScaleAndCrop(800, 600)
.AddImageWatermark(@"wwwroot\images\icon.png")
.AddTextWatermark("http://ziyad.info")
.SaveAs($"wwwroot\\images\\{file.FileName}");
}
}
}
}
All resizing methods will return a System.Drawing.Image
file that can be saved in any supported image format (JPG, PNG, etc.)
- Scale : Auto scales image by width or height, and keeps aspect ratio same as original image
img.Scale(800, 600);
// or
img.Scale(800, 600, new GraphicOptions { ... });
- Scale by width : Scales image by provided width value, auto adjusts new height according to aspect ratio.
img.ScaleByWidth(800);
// or
img.ScaleByWidth(800, new GraphicOptions { ... });
- Scale by height : Scales image by provided height value, auto adjusts new width according to aspect ratio.
img.ScaleByHeight(600);
// or
img.ScaleByHeight(600, new GraphicOptions { ... });
- Scale and crop : Scalesthe image to fit new width or new height (which fits first), then crops out the rest of the image.
img.ScaleAndCrop(800, 600);
// or
img.ScaleAndCrop(800, 600, TargetSpot.Center);
// or
img.ScaleAndCrop(800, 600, new GraphicOptions { ... });
// or
img.ScaleAndCrop(800, 600, new GraphicOptions { ... }, TargetSpot.Center);
- Crop : Directly crop a specified spot of the image, without scaling.
img.Crop(800, 600);
// or
img.Crop(800, 600, TargetSpot.Center);
// or
img.Crop(800, 600, new GraphicOptions { ... });
// or
img.Crop(800, 600, new GraphicOptions { ... }, TargetSpot.Center);
ImageResize supports adding text and image watermarks, both can be placed to any specified spot with ability to change opacity of the text or the image.
Below code will draw a colored text with a transparent background in the bottom left corner of the uploaded image:
img.AddTextWatermark("http://ziyad.info");
// or
img.AddTextWatermark("http://ziyad.info", new TextWatermarkOptions { ... });
img.AddImageWatermark(@"wwwroot\images\logo.png");
// or
img.AddImageWatermark(@"wwwroot\images\logo.png", new ImageWatermarkOptions { ... });
// or
var wm = Image.FromFile(@"wwwroot\images\logo.png");
img.AddImageWatermark(wm);
// or
var wm = Image.FromFile(@"wwwroot\images\logo.png");
img.AddImageWatermark(wm, new ImageWatermarkOptions { ... });
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 }
Define graphic options to ensure maximum image compatibility and quality. See GraphicOptions
Define text watermark options, like locaiton, color, text outline, etc. See TextWatermarkOptions
Define image watermark option, lie location, opacity and margin. See ImageWatermarkOptions
goto project website: http://ziyad.info/en/articles/29-LazZiya_ImageResize
https://github.com/LazZiya/ImageResize/blob/master/LICENSE