meta info
- Title: LazZiya.ImageResize - Conditional Methods
- Keywords: asp.net-core, image, resize, crop, scale, text watermark, animated, gif, conditional
- Description: Image resizing tool for .Net applications to resize images and add text/image watermark, Supports most common image types including animated gif.
- Author: Ziya Mollamahmut
- Date: 09-Mar-2021
- Image: https://github.com/LazZiya/Docs/raw/master/LazZiya.ImageResize/v4.0/images/lazziya-imageresize-logo.png
- Image-alt: LazZiya.ImageResize Logo
- Version: v4.0
Just like all methods, with additional condition boolean parameter. If the parameter is true the resize will done, otherwise it will return the same image.
In some cases we may need to resize an image depending on a dynamic parameter in our project.
using(var img = Image.FromFile("my-image-file.jpg")
{
if(doResize == true)
{
img.Scale(800, 600)
.SaveAs("new-image.jpg");
}
else
{
img.SaveAs("new-image.jpg");
}
}
using(var img = Image.FromFile("my-image-file.jpg")
{
img.ScaleIf(doResize, 800, 600)
.SaveAs("new-image.jpg");
}
Conditional reaize can be applied to all resizing methods, and all can be chained together:
// doResize, addTextWM and addImgWM are boolean values
using(var img = Image.FromFile("my-image-file.jpg")
{
img.ScaleIf(doResize, 800, 600)
.AddTextWatermarkIf(addTextWM, "https://docs.ziyad.info")
.AddImageWatermarkIf(addImagWm, "logo.png")
.SaveAs("new-image.jpg");
}
All methods are available with the conditional If
variation.