This project has moved. For the latest updates, please go here.

How to overlay a tiled pattern on a solid background?

Jan 8, 2014 at 6:56 PM
Edited Jan 9, 2014 at 6:56 AM
I have a background/canvas with a solid color. I want to overlay a pattern on top of that background. I want to set the opacity of the tiled pattern too.

This is what I have Compose using Image Tiles
    MagickImage pattern = new MagickImage("connect.png");
    pattern.Alpha(AlphaOption.Set);
    pattern.QuantumOperator(Channels.Alpha, EvaluateOperator.Set, Quantum.Max / 2);

    using (MagickImage wallpaper = new MagickImage(new MagickColor("#2980b9"),1440,900)) {
        wallpaper.Composite(pattern,Gravity.Center,CompositeOperator.Dissolve,"50");
        wallpaper.Write("test.png");
    }
    pattern.Dispose();
I am getting a solid background and the pattern is center. How do I tile the image? What else do I have to do? How do I do -tile?
Coordinator
Jan 8, 2014 at 8:46 PM
Can you post a message with a command line example and a link to your image? I don't really understand what you are trying to do.
Jan 8, 2014 at 8:57 PM
Edited Jan 9, 2014 at 7:02 AM
original pattern :

Image

background color of base canvas is #2980b9.

Output Image of the code above is

Image


I want to achieve this:

Image


I am not sure how to do this in command line but in order to tile the square image on the center from the first link . I must use the -tile command line option to compose the tiled background as explained here http://www.imagemagick.org/Usage/canvas/#tile.
But, I don't know how to execute the tile parameter in this library. I hope you can help me. Thanks for replying.
Coordinator
Jan 8, 2014 at 9:09 PM
There is not support for the tile operator in Magick.NET yet. You could accomplish the same output image if you tile the image yourself. You could start with an output image of the same size as your original image and use the MagickImageCollection class to combine your images. You can create a duplicate with the Clone method. And then use AppendHorizontally/AppendVertically of the MagickImageCollection to combine your images. I will look into ImageMagick to see how the tile command works and see if I can add it.
Jan 8, 2014 at 9:30 PM
dlemstra wrote:
There is not support for the tile operator in Magick.NET yet. You could accomplish the same output image if you tile the image yourself. You could start with an output image of the same size as your original image and use the MagickImageCollection class to combine your images. You can create a duplicate with the Clone method. And then use AppendHorizontally/AppendVertically of the MagickImageCollection to combine your images. I will look into ImageMagick to see how the tile command works and see if I can add it.
Thanks for clearing that out, I finally used gdi+ for the tiling part
Coordinator
Jan 9, 2014 at 6:49 AM
Edited Jan 11, 2014 at 7:47 AM
It turns out there is already a method called Texture for what you are trying to do. You can accomplish the output image with the following code:
using (MagickImage pattern = new MagickImage("connect.png"))
{
  pattern.Alpha(AlphaOption.Set);
  pattern.QuantumOperator(Channels.Alpha, EvaluateOperator.Set, Quantum.Max / 2);

  using (MagickImage wallpaper = new MagickImage(new MagickColor("#2980b9"), 1440, 900))
  {
    wallpaper.Texture(pattern);
    wallpaper.Write("test.png");
  }
}
p.s. Can you stop marking this question as the answer, it looks strange on the discussion overview.
Jan 9, 2014 at 6:58 AM
Edited Jan 9, 2014 at 6:59 AM
Thanks for your help , that indeed is cleaner. This is how I did it with gdi+ if anyone is interested
    Bitmap tilePatternBitmap = new Bitmap("connect.png");
    Bitmap tiledPatternBitmap = new Bitmap(1440, 900);

    using (TextureBrush brush = new TextureBrush(tilePatternBitmap, WrapMode.Tile))
    using (Graphics g = Graphics.FromImage(tiledPatternBitmap))
    {
        g.FillRectangle(brush, 0, 0, tiledPatternBitmap.Width, tiledPatternBitmap.Height);
    }

    MagickImage pattern = new MagickImage(tiledPatternBitmap);
    pattern.Alpha(AlphaOption.Set);
    pattern.QuantumOperator(Channels.Alpha, EvaluateOperator.Set, Quantum.Max / 2);

    using (MagickImage wallpaper = new MagickImage(new MagickColor("#2980b9"),1440,900)) {
        wallpaper.Composite(pattern, Gravity.Center, CompositeOperator.Dissolve, "50");
        wallpaper.Write("test.png");
    }
    pattern.Dispose();