24 Dec

Relm: alter your image nuget package

Relm is a lightweight NuGet package used to alter your images or byte arrays.
Crop your image, change its size and type or compress them to save disk space.

Contents



Installation

The package can be found on the NuGet server when you look for “Relm” https://www.nuget.org/packages/Relm/.
Install it in your project and you’re good to go.

Image and Byte Array

Relm can be used for both images and byte arrays of images. All functions can be used for both types.
If needed you can also change this types in code:

public void DoSomething(Image image)
{
    var bytearray = image.ToByteArray();
    image = bytearray.ToImage();
}

Change Size

Relm can be used to adjust the size of you images.
Change the height and width of your images to save disk space on your application or website.

By default you can change the width or height and the ratio will be maintained. This means if you change the width of a 100×100 to 50, the height will also change to 50.

public Image DoWidth(Image image)
{
    return image.SetWidth(200);
}

public Image DoHeight(Image image)
{
    return image.SetHeight(200);
}

Of course you can also change the width and make sure that the height stays what it is. For this you have the make sure the ratio is not maintained.

public Image DoWidth(Image image)
{
    //Set width and do not maintain the ratio
    return image.SetWidth(200, false);
}


Not every image is the same, and sometimes you want to make sure that when you change the width or height of a small image it is not stretched. Luckily this is done by default.
But sometimes you would not mind that an image is stretched, this can be done by adding a last variable and set it to true

public Image DoWidth(Image image)
{
    //Set width, maintain the ratio and allow the image to be stretched.
    return image.SetWidth(200, false, true);
}

 
All of the above can of course also be done with an image in byte array.

public Image DoWidth(byte[] image)
{
    return image.SetWidth(200);
}

Crop

Images and byte arrays can be cropped. This can be done from 9 different positions. By default this position is set from the middle center.
Cropping an image can be done by the following code:

public Image DoCrop(Image image)
{
    return image.Crop(100, 200, CropPosition.TopCenter);
}

Compress

Relm also has the possibility to compress the image size. This is done by lowering the quality size.
By default this is done to 80%. This lowers the quality, but it’s not visible on most images. You can of course change this number to what you desire, but be warned that lowering it to less than 80 will visibly lower the image quality.

public Image DoCrompress(Image image)
{
    //Compress to default, 80
    image = image.Compress();
    //Compress to 50
    image = image.Compress(50);
}

Change Type

To change the image type, let’s say change a PNG to a JPG this can be done by adding this code:

public Image DoSomething(Image pngImage)
{
    return pngImage.ToJpg();
}

Images can be converted to JPG, PNG, GIF and ICON.



Release Notes

1.0.1 – Initial Release

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.