Image Resize 3.0 for .Net Applications

This page will demonstrate different samples for ImageResize v3.0 if you are looking for version 2.0 click here.

Related article : Image Resize for NetCore
Github repository : LazZiya.ImageResize

Scale by Width

Top

Scale down by given width parameter and keep original aspect ratio


using System.Drawing;
using LazZiya.ImageResize;

// use using to dispose the image after resize and free memory
using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.ScaleByWidth(600)
        .SaveAs(@"wwwroot\images\new-istanbul-panoramic.jpg");
}
        
Result image size 600x228

Scale by Height

Top

Scale down by given height parameter and keep original aspect ratio


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.ScaleByHeight(300)
        .SaveAs(@"wwwroot\images\H300-istanbul-panoramic.jpg");
}
        
Result image size: 786x300

Scale

Top

Auto scale down image till longest border is equal to the target dimension and keep original aspect ratio


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.Scale(600, 400)
        .SaveAs(@"wwwroot\images\scale-istanbul-panoramic.jpg");
}
        

Result image size: 600x228


Scale and crop

Top

Scale down by width and height till shortest border is equal to the requested value, then crop the extras. Final image will have the exact given new width and height but could have different aspect ratio depending on new dimensions.


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.ScaleAndCrop(600, 400)
        .SaveAs(@"wwwroot\images\scale-istanbul-panoramic.jpg");
}
        
Result image size: 600x400

Scale and crop target spot

Top

By default ScaleAndCrop will target the image center. Additionally it is possible to specify different area of the image to be scaled/cropped and generate the final result. You can choose from pre-defined 9 spots (3 rows, 3 columns) using TargetSpot attribute


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.ScaleAndCrop(600, 400, TargetSpot.MiddleRight)
        .SaveAs(@"wwwroot\images\scale-crop-middle-right-istanbul-panoramic.jpg");
}
        
Result image size: 600x400

Crop

Top

Crop the image by given sizes.


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.Crop(600, 600)
        .SaveAs(@"wwwroot\images\crop-istanbul-panoramic.jpg");
}
        
Result image size: 600x600

Crop target spot

Top

Crop specified spot from the the image by given sizes. By default image center will be cropped, but it is possible to target different area of the image to be cropped:


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.Crop(600, 600, TargetSpot.BottomRight)
        .SaveAs(@"wwwroot\images\crop-bottom-right-istanbul-panoramic.jpg");
}
        
Result image size: 600x600

Text Watermark

Top

Add text watermark to the image. In below sample first I will resize the image then add text watermark to the resized image.


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.Crop(500, 500)
        .AddTextWatermark("http://demo.ziyad.info")
        .SaveAs(@"wwwroot\images\text-watermark-istanbul-panoramic.jpg");
}
        
Result image size: 500x500

Text Watermark Options

Top

Use TextWatermarkOptions to customize settings of the text watermark.


var twmOps = new TextWatermarkOptions
{
    Location = TargetSpot.TopRight,

    FontSize = 50,

    FontName = "Edwardian Script ITC",

    // Text with red color and half opacity
    TextColor = Color.FromArgb(200, Color.Red),

    // Use alpha channel to specify color opacity
    // e.g. use 0 opacity to disable drawing the outline
    OutlineColor = Color.FromArgb(0, Color.White),
};

using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    img.Crop(500, 500)
        .AddTextWatermark("demo.ziyad.info", twmOps)
        .SaveAs(@"wwwroot\images\text-watermark-ops-istanbul-panoramic.jpg");
}
        
Result image

Image Watermark

Top

Draw image watermark over the image. First we will resize the image to 500x500 then watermark will be applied.


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    var iwm = Image.FromFile(@"wwwroot\images\wm.png");

    img.Crop(500, 500)
        .AddImageWatermark(iwm)
        .SaveAs(@"wwwroot\images\image-watermark-istanbul-panoramic.jpg");

    iwm.Dispose();
}
        
Result image

Image Watermark Opitons

Top

Use ImageWatermarkOptions to customize settings of the image watermark.


var iwmOps = new ImageWatermarkOptions
{
    Location = TargetSpot.BottomRight,
                
    // 0 full transparent, 100 full color
    Opacity = 50,
};

using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    var iwm = Image.FromFile(@"wwwroot\images\wm.png");

    img.Crop(500, 500)
        .AddImageWatermark(iwm, iwmOps)
        .SaveAs(@"wwwroot\images\image-watermark-ops-istanbul-panoramic.jpg");

    iwm.Dispose();
}
        
Result image

If you need to resize the watermark, use same pethods to resize it then save it to the disc and then use Image.FromFile as described above.


Combining resize settings

Top

As mentioned earlier, all methods can be chained together to provide easily image processing.

Additionally, LazZiya.ImageResize supports multilingual texts as shown below.


var twmOps = new TextWatermarkOptions
{
    Location = TargetSpot.Center,

    FontSize = 35,

    FontStyle = FontStyle.Bold,

    // Don't draw text
    TextColor = Color.FromArgb(0, Color.White),

    // Draw outline only, alpha (0 - 255)
    OutlineColor = Color.FromArgb(125, Color.BlueViolet),

    OutlineWidth = 2
};

var iwmOps = new ImageWatermarkOptions
{
    Location = TargetSpot.BottomMiddle,
                
    // 0 full transparent, 100 full color
    Opacity = 30,
};


using (var img = Image.FromFile(@"wwwroot\images\istanbul-panoramic.jpg"))
{
    var iwm = Image.FromFile(@"wwwroot\images\wm.png");

    img.Crop(500, 500)
        .AddImageWatermark(iwm, iwmOps)
        .AddTextWatermark("Türkçe 中國 العربية", twmOps)
        .SaveAs(@"wwwroot\images\image-text-istanbul-panoramic.jpg");

    iwm.Dispose();
}