Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / WPF
Tip/Trick

Generative Art III: Work With Image Tiles

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
2 Jan 2015CPOL 14K   256   13   1
Image sections and images: fundamental C# classes and methods for the artwork production in the area of generative art.

Introduction

This third part of the series deals with generative art based on sections of images.

Background

The basis of the representational example in this post are the two following photos:

Image 1

"Cold Inside" © 2014 Victoria C. Rhodes

Image 2

"Hope Road" © 2014 Victoria C. Rhodes

Basic Approach

For producing an artwork based on sections of images, you can:

  • create a canvas with a first input image
  • add image sections of a second input image onto it
C#
private static string inputFileBackground = "Victoria C Rhodes Hope Road.jpg";
private static string inputFile = "Victoria C Rhodes Cold Inside.jpg";
private static string currentDirectory = Environment.CurrentDirectory + @"\";
private static BitmapImage inputImageBackground = new BitmapImage
    (new Uri(currentDirectory + inputFileBackground, UriKind.Absolute));
private Brush backgroundColor = new ImageBrush() { ImageSource = inputImageBackground };

public MainWindow()
{
    InitializeComponent();

    var canvas = new GenArtCanvas(inputImageBackground.Width, 
    inputImageBackground.Height, backgroundColor);

    canvas.Generate(currentDirectory + inputFile);
    canvas.Save(currentDirectory + title + format);
}

Generate Artwork

This algorithm cuts out sections (squares in this case) following a regular pattern:

C#
public void Generate(string InputImage)
{
    Generator.ImageBasedSections(this, InputImage, Width, Height, 64); // <- number of squares per line
}

public static void ImageBasedSections(Canvas Canvas, string InputImage2, 
    double Width, double Height, int NumberOfSquares)
{
    double SquareSize = ((int)(Width / NumberOfSquares)) / 2;

    for (var row = SquareSize * 0.5; row <= Height - SquareSize * 0.5; row += SquareSize * 2)
        for (var column = SquareSize * 0.5; column <= Width - SquareSize * 0.5; 
        column += SquareSize * 2)
        {
            var shape = new Rectangle() //new Ellipse()
            {
                Width = SquareSize,
                Height = SquareSize,
                Fill = Section(InputImage2, Width, Height, SquareSize, row, column),
            };
            Canvas.Children.Add(shape);
            Canvas.SetLeft(shape, column);
            Canvas.SetTop(shape, row);
        }
}

Each section is made of an ImageBrush with a specific Viewbox and Viewport:

C#
private static ImageBrush Section(string InputImage2, double ImageWidth, 
    double ImageHeight, double SquareSize, double Row, double Column)
{
    return new ImageBrush()
    {
        ImageSource = new BitmapImage(new Uri(InputImage2, UriKind.Absolute)), 
        Viewbox = new Rect(Column, Row, SquareSize, SquareSize),
        ViewboxUnits = BrushMappingMode.Absolute,
        Viewport = new Rect(0, 0, ImageWidth, ImageHeight),
        ViewportUnits = BrushMappingMode.Absolute,
        Stretch = Stretch.None,
        AlignmentX = AlignmentX.Left,
        AlignmentY = AlignmentY.Top
    };
}

The generated result looks like this:

Image 3

Changing the number of squares per line from 64 to 16 respectively 4 produces new results:

Image 4

Image 5

Points of Interest

A similar approach is used for a "woven collage".

Image 6

C#
public static void WovenCollage(Canvas Canvas, string InputImage2, 
    double Width, double Height, int NumberOfSquares)
{
    int SquareSize = (int)(Width / NumberOfSquares);

    for (var row = 0; row < Height; row += SquareSize)
        for (var column = 0; column < Width; column += SquareSize * 2)
        {
            var shape = new Rectangle() //new Ellipse()
            {
                Width = SquareSize,
                Height = SquareSize,
                Fill = Section(InputImage2, Width, Height, SquareSize, 
            row, column + row % (2 * SquareSize)),
            };
            Canvas.Children.Add(shape);
            Canvas.SetLeft(shape, column + row % (2 * SquareSize));
            Canvas.SetTop(shape, row);
        }
}

Image 7

Image 8

History

  • 2nd January, 2015 - Published
  • 11th January, 2015 - Links to other articles in the series added
  • 25th January, 2015 - Klimt example added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
schoder.uk
United Kingdom United Kingdom
generative artist: schoder.uk

Comments and Discussions

 
Generalthanks Pin
Hooman_Kh5-Jan-15 12:45
Hooman_Kh5-Jan-15 12:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.