Imagesharp: Change background color when ResizeMode.Pad

Created on 15 Feb 2018  路  4Comments  路  Source: SixLabors/ImageSharp

Description

It's possible to change the background color when resizing image with mode Pad. Now is always black, but I would like to change it to other colors.

Steps to Reproduce

newImage.Mutate(x => x
                     .Resize(new SixLabors.ImageSharp.Processing.ResizeOptions
                     {
                         Size = new SixLabors.Primitives.Size(newWidth, newHieght),
                         Mode = SixLabors.ImageSharp.Processing.ResizeMode.Pad                         
                     })
                     );
  • ImageSharp version: 1.0.0-beta0002
  • Environment (Operating system, version and so on): Windows/Linux
  • .NET Framework version: .net core 2.0
enhancement good first issue question up-for-grabs

Most helpful comment

Solution

Searching inside the package I found the following extension "BackgroundColor" that meet my needs.

Example

newImage.Mutate(x => x
                     .Resize(new SixLabors.ImageSharp.Processing.ResizeOptions
                     {
                         Size = new SixLabors.Primitives.Size(newWidth, newHieght),
                         Mode = SixLabors.ImageSharp.Processing.ResizeMode.Pad                         
                     })
                     .BackgroundColor(new Rgba32(12, 12, 221))
                     );

All 4 comments

Solution

Searching inside the package I found the following extension "BackgroundColor" that meet my needs.

Example

newImage.Mutate(x => x
                     .Resize(new SixLabors.ImageSharp.Processing.ResizeOptions
                     {
                         Size = new SixLabors.Primitives.Size(newWidth, newHieght),
                         Mode = SixLabors.ImageSharp.Processing.ResizeMode.Pad                         
                     })
                     .BackgroundColor(new Rgba32(12, 12, 221))
                     );

Reopening as the solution can be improved upon.

We should add a parameter to the explicit Pad method to allow a shortcut to setting the color.

.Mutate(x => x.Pad(int width, int height, TPixel color))

.Mutate(x => x.Pad(GraphicsOptions options, int width, int height, TPixel color))

Resize itself will probably stay the same as we don't want to make ResizeOptions a generic type.

1 year later and I've decided against merging the two methods as it adds internal complexity that I don't think is worth it when you can simply chain commands like above.

Actually. I'm being an idiot. Gimme 5 minutes.

Was this page helpful?
0 / 5 - 0 ratings