Search code examples
c#imagemagickimagickimagemagick-convert

ImageMagick creating blank transparent square(s) according to width


Hey all I am trying to fill in the area with blank circles, but its turning out looking like this:

(Sized down in order not to take up so much room here. Original size: 360x1200). Also note that I do not really use the blank.png file - its just there so I can check to see if its being used or not. I'm making just a plain color box as the "blank.png".

enter image description here

My code:

using (MagickImageCollection images = new MagickImageCollection())
        {
            List<string> lFiles = new List<string>();

            lFiles.Add(@"C:\Users\David\Pictures\1.jpg");
            lFiles.Add(@"C:\Users\David\Pictures\blank.png");
            lFiles.Add(@"C:\Users\David\Pictures\blank.png");
            lFiles.Add(@"C:\Users\David\Pictures\blank.png");
            lFiles.Add(@"C:\Users\David\Pictures\blank.png");

            IMagickImage roundImg = new MagickImage();
            IMagickImage mask = new MagickImage();
            IMagickImage shadow = new MagickImage();
            IMagickImage result = new MagickImage();
            bool isBlankImage = false;

            foreach (string tempFBProfileImg in lFiles)
            {
                roundImg = new MagickImage(tempFBProfileImg);

                if (Regex.IsMatch(@"C:\Users\David\Pictures\blank.png", @"\bblank.png\b"))
                {
                    roundImg = new MagickImage(MagickColors.White, 100, 100);
                    roundImg.Resize(100, 100);
                    roundImg.Transparent(MagickColors.White);
                }
                else
                {
                    mask = new MagickImage("xc:black", 100, 100);
                    mask.Settings.FillColor = MagickColors.White;
                    mask.Draw(new DrawableCircle(50, 50, 50, 90));
                    mask.HasAlpha = false;

                    roundImg.Resize(100, 100);
                    roundImg.Composite(mask, CompositeOperator.CopyAlpha);
                    roundImg.Draw(
                        new DrawableStrokeColor(MagickColors.Black),
                        new DrawableStrokeWidth(1),
                        new DrawableFillColor(MagickColors.None),
                        new DrawableCircle(50, 50, 50, 90)
                   );

                    shadow = new MagickImage("xc:none", 100, 100);
                    shadow.Settings.FillColor = MagickColors.Black;
                    shadow.Draw(new DrawableCircle(50, 50, 50, 90));
                    shadow.Blur(0, 5);
                    roundImg.Composite(shadow, CompositeOperator.DstOver);
                }

                images.Add(roundImg);
                images.First().BackgroundColor = MagickColors.None;
                result = images.SmushHorizontal(-35);
                result.Resize(360, 0);
                result.Write(@"C:\Users\David\Pictures\final.png");
            }
        }

In the above code, I am creating a white 100x100 square. Then im resizing that to 100x100 and turning the white background transparent for the blank image.

The error I get is:

'width or height exceeds limit `#FFFFFFFFFFFF' @ error/cache.c/OpenPixelCache/3491'

on the result.Write(@"C:\Users\David\Pictures\final.png"); line.

When I have just this code running:

MagickImage roundImg = new MagickImage(MagickColors.White, 100, 100);
roundImg.Resize(100, 100);
roundImg.Transparent(MagickColors.White);
roundImg.Write(@"C:\Users\David\Pictures\aloneTest.png");

It seems to work just fine...

enter image description here

How can I make this work as I am needing it too?

Images used:


enter image description here


Blank.png start ----

enter image description here

Blank.png end ----

What I am wanting it to look like is this:

enter image description here

which really looks like this, since blank.png is transparent:

enter image description here

The width will be different depending on how many blank.png images are needed to be inserted to get that width. The example above has 5 images of which 4 are the blank ones.

Using Bonzos example in C#:

 roundImg.Resize(new MagickGeometry(100, 100));
 roundImg.BackgroundColor = MagickColors.Transparent;
 roundImg.Extent(360, 100, Gravity.West);
 result = roundImg;

Produces just a transparent 360x100 image.

Tried fmw42:

 mask = new MagickImage("xc:black", 100, 100);
 mask.Settings.FillColor = MagickColors.White;
 mask.Draw(new DrawableCircle(50, 50, 50, 100));
 mask.HasAlpha = false;
 mask.Resize(100, 100);
 roundImg.Composite(mask, CompositeOperator.CopyAlpha);

Possible solution

 if (Regex.IsMatch(tempFBProfileImg.ToLower(), @"\bblank.png\b"))
 {
    result.Extent(360, 100, Gravity.West);
    images.Add(result);
    break;
 }

which results in:

enter image description here


Solution

  • Why do you not use extent with a transparent background to increase the canvas size?

    I do not know c# but in command line it would be in this format:

    convert ZME5U.jpg -background transparent -gravity west -extent 800x284 result.png
    

    Extended canvas