Imageprocessor: BackgroundColor not applied when resizing image with Pad

Created on 4 Jan 2019  路  4Comments  路  Source: JimBobSquarePants/ImageProcessor

Description

I'm writing a batch processor that will convert portrait image to a 16:9 format, and everything is working fine, except the background color is always transparent, which I absolutely do not want. My code is below.

            using (var currentImage = new ImageProcessor.ImageFactory())
            {
                currentImage.Load(fileName);
                int width = currentImage.Image.Width;
                int height = currentImage.Image.Height;
                if (height > width)
                {
                    int newWidth = (height / 9) * 16;
                    Size newSize = new Size(newWidth, height);
                    currentImage.BackgroundColor(Color.FromArgb(0, 177, 64));
                    ImageProcessor.Imaging.ResizeLayer rlayer = new ImageProcessor.Imaging.ResizeLayer(newSize, ImageProcessor.Imaging.ResizeMode.Pad, ImageProcessor.Imaging.AnchorPosition.Center, true);
                    currentImage.Resize(rlayer);
                }
                currentImage.Image.Save(fileName);
            }

System Configuration


Visual Studio 2017

All 4 comments

Interestingly this code works, but isn't an ideal solution.

Changes are:
I implicitly set the format to Jpeg.
Then call ReplaceColor(Color.Black, Color.FromArgb(0, 177, 64)); after the resize. This does cause some additional problems though, as it replace some pixels in the actual image.

            string newFileName = Path.Combine(file.DirectoryName, "output", file.Name);

            using (var currentImage = new ImageProcessor.ImageFactory())
            {
                currentImage.Load(file.FullName).Format(new JpegFormat() { Quality= 100 });
                int width = currentImage.Image.Width;
                int height = currentImage.Image.Height;
                if (height > width)
                {
                    int newWidth = (height / 9) * 16;
                    Size newSize = new Size(newWidth, height);
                    ImageProcessor.Imaging.ResizeLayer rlayer = new ImageProcessor.Imaging.ResizeLayer(newSize, ImageProcessor.Imaging.ResizeMode.Pad, ImageProcessor.Imaging.AnchorPosition.Center);
                    currentImage.Resize(rlayer).ReplaceColor(Color.Black, Color.FromArgb(0, 177, 64));
                }
                currentImage.Save(newFileName);
            }

The API is imperative, you have to do things in the correct order.

  1. Resize
  2. BackgroundColor

Doing the reverse makes no sense, you have to create the padding pixels before setting a background color.

This does cause some additional problems though, as it replace some pixels in the actual image.

If you have transparent pixels in the original image I don't understand why you would expect them not to be filled in.

P.S Don't delete the issue template, it's incredibly rude.

Ridiculous,

Have you ever used image editing software before?

You always set the background color first when adjusting the canvas, which is essentially what I am doing here.

That being said, I would have done so had the documentation actually mentioned it, but it doesn't.

This does cause some additional problems though, as it replace some pixels in the actual image.

If you have transparent pixels in the original image I don't understand why you would expect them not to be filled in.

First of all, you should read before you comment, they were not transparent pixels being replaced it was Black, second of all it behaved exactly I as I expected and replaced all the black pixels which is why I said it was not ideal.

p.s. Your entire response was incredibly rude.

Ever used a fluent API before?
Written a graphics library?
Bothered to read the source code?

Look up what Graphics.Clear does, Graphics.DrawImage also and you鈥檒l see you鈥檙e talking nonesense.

Next time someone calls you out for ignoring obvious instructions don鈥檛 have a hissy fit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xumix picture xumix  路  10Comments

mortenbock picture mortenbock  路  3Comments

AtomicSpider picture AtomicSpider  路  3Comments

nul800sebastiaan picture nul800sebastiaan  路  7Comments

Leftyx picture Leftyx  路  5Comments