Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Graphics.doc
Скачиваний:
20
Добавлен:
16.11.2019
Размер:
3.1 Mб
Скачать

Компиляция кода

Предыдущий пример предназначен для работы с Windows Forms, для него необходим объект PaintEventArgs e, передаваемый в качестве параметра обработчику события Paint.

How to: Tile a Shape with an Image

Just as tiles can be placed next to each other to cover a floor, rectangular images can be placed next to each other to fill (tile) a shape. To tile the interior of a shape, use a texture brush. When you construct a TextureBrush object, one of the arguments you pass to the constructor is an Image object. When you use the texture brush to paint the interior of a shape, the shape is filled with repeated copies of this image.

The wrap mode property of the TextureBrush object determines how the image is oriented as it is repeated in a rectangular grid. You can make all the tiles in the grid have the same orientation, or you can make the image flip from one grid position to the next. The flipping can be horizontal, vertical, or both. The following examples demonstrate tiling with different types of flipping.

To tile an image

  • This example uses the following 75×75 image to tile a 200×200 rectangle.

  • The following illustration shows how the rectangle is tiled with the image. Note that all tiles have the same orientation; there is no flipping.

Image image = new Bitmap("HouseAndTree.gif");

TextureBrush tBrush = new TextureBrush(image);

Pen blackPen = new Pen(Color.Black);

e.Graphics.FillRectangle(tBrush, new Rectangle(0, 0, 200, 200));

e.Graphics.DrawRectangle(blackPen, new Rectangle(0, 0, 200, 200));

Мозаичное заполнение фигуры заданным изображением

По аналогии с укладкой мозаики одна к другой при заполнении пола прямоугольные изображения можно укладывать одно к другому, чтобы заполнить фигуру. Для мозаичного заполнения внутренней части фигуры используется текстурная кисть. При создании объекта TextureBrush одним из передаваемых конструктору параметров является объект Image. При использовании текстурной кисти для заполнения внутренней части фигуры заполнение осуществляется повторяющимися копиями данного изображения.

Свойство "режим обертывания" объекта TextureBrush определяет способ ориентации изображения при его тиражировании на прямоугольной сетке. Можно использовать одну и ту же ориентацию для всех изображений — элементов этой сетки, можно также осуществлять зеркальное отображение изображения от позиции к позиции сетки. Зеркальное отображение может быть горизонтальным, вертикальным или объединением этих вариантов. В приведенных ниже примерах демонстрируется мозаичное заполнение с использованием различных типов зеркального отображения.

Мозаичное заполнение изображением

  • В данном примере используется приведенное ниже изображение размером 75×75 для мозаичного заполнения прямоугольника размером 200×200.

  • На следующем рисунке показано, как прямоугольник заполняется изображением (мозаичное заполнение). Обратите внимание, что все элементы мозаики имеют одинаковую ориентацию, зеркальное отображение не используется.

-------------------

To flip an image horizontally while tiling

  • This example uses the same 75×75 image to fill a 200×200 rectangle. The wrap mode is set to flip the image horizontally. The following illustration shows how the rectangle is tiled with the image. Note that as you move from one tile to the next in a given row, the image is flipped horizontally.

Image image = new Bitmap("HouseAndTree.gif");

TextureBrush tBrush = new TextureBrush(image);

Pen blackPen = new Pen(Color.Black);

tBrush.WrapMode = WrapMode.TileFlipX;

e.Graphics.FillRectangle(tBrush, new Rectangle(0, 0, 200, 200));

e.Graphics.DrawRectangle(blackPen, new Rectangle(0, 0, 200, 200));

To flip an image vertically while tiling

  • This example uses the same 75×75 image to fill a 200×200 rectangle. The wrap mode is set to flip the image vertically.

Image image = new Bitmap("HouseAndTree.gif");

TextureBrush tBrush = new TextureBrush(image);

Pen blackPen = new Pen(Color.Black);

tBrush.WrapMode = WrapMode.TileFlipY;

e.Graphics.FillRectangle(tBrush, new Rectangle(0, 0, 200, 200));

e.Graphics.DrawRectangle(blackPen, new Rectangle(0, 0, 200, 200));