LazZiya.ImageResize 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package LazZiya.ImageResize --version 3.0.0
NuGet\Install-Package LazZiya.ImageResize -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LazZiya.ImageResize" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LazZiya.ImageResize --version 3.0.0
#r "nuget: LazZiya.ImageResize, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install LazZiya.ImageResize as a Cake Addin
#addin nuget:?package=LazZiya.ImageResize&version=3.0.0

// Install LazZiya.ImageResize as a Cake Tool
#tool nuget:?package=LazZiya.ImageResize&version=3.0.0

LazZiya.ImageResize

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).

Demo:

http://demo.ziyad.info/en/ImageResize

Latest version

Installation:

Install via nuget (enable Include prerelease to check for latest test versions):

Install-Package LazZiya.ImageResize

Resize image file

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");
}

Add text watermark and change color, opacity, ...etc.

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");
}

Add image watermark and change location, opacity, ...etc.

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");
}

Upload and resize an image

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}");
            }
        }
    }
}

Supported resizing methods

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);

Adding Watermark

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.

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.AddTextWatermark("http://ziyad.info");

// or
img.AddTextWatermark("http://ziyad.info", new TextWatermarkOptions { ... });

Add image watermark and adjust opacity :

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 { ... });

TargetSpot :

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 }

GraphicOptions

Define graphic options to ensure maximum image compatibility and quality. See GraphicOptions

TextWatermarkOptions

Define text watermark options, like locaiton, color, text outline, etc. See TextWatermarkOptions

ImageWatermarkOptions

Define image watermark option, lie location, opacity and margin. See ImageWatermarkOptions

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on LazZiya.ImageResize:

Package Downloads
OpenXMLSDK.Engine

This package contains the 'Open-XML-SDK' plugin for MvvmCross.

HT.Image

Resize, crop, add text watermark to Images: var file = await model.Scale(width, height,true); // model is IFormFile file.SaveAs("wwwroot/" + pathFile);

HT.SQLServer.File

Resize, crop, add text watermark to Images: var file = await model.Scale(width, height,true); // model is IFormFile file.SaveAs("wwwroot/" + pathFile);

TestBin

test file manager - just for test

SaClass

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.4 36,706 1/29/2023
4.1.3 1,915 1/8/2023
4.1.1 8,885 10/28/2022
4.1.0 89,082 8/2/2021
4.0.0 11,611 6/11/2021
4.0.0-preview4 1,095 3/11/2021
4.0.0-preview3 620 3/9/2021
4.0.0-preview2 652 2/22/2021
4.0.0-preview1 1,205 2/10/2021
3.0.2 35,667 6/8/2020
3.0.1 3,580 4/26/2020
3.0.0 31,770 12/18/2019
3.0.0-preview2 879 12/15/2019
3.0.0-preview1 876 12/8/2019
2.0.0 9,049 2/28/2019
1.0.0 1,181 2/17/2019

- .NetStandard 2.0 support
     - New: TextOutline for text watermark
     - New: AddTextWatermark methods
     - New: AddImageWatermark methods
     - New: Graphic options for image resize methods
     - CMYK support (fix for issue https://github.com/LazZiya/ImageResize/issues/3)
     - Obsolete: TextWatermark, ImageWatermark extensions are obsolete and will be removed in a feature release
     - Overall performance improvments