meta info

By Ziya Mollamahmut

Conditional Methods

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.

Why Conditional Methods?

In some cases we may need to resize an image depending on a dynamic parameter in our project.

The old way of doing it:

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

The new way:

using(var img = Image.FromFile("my-image-file.jpg")
{
    img.ScaleIf(doResize, 800, 600)
       .SaveAs("new-image.jpg");
}

Chain Methods

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.