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

Загрузка и отображение метафайлов

Класс Metafile, наследующий у класса Image, содержит методы для записи, отображения и проверки векторных изображений.

Пример

Для отображения векторного изображения (метафайла) на экране следует использовать объекты Metafile и Graphics. Имя файла (или потока) передается в качестве параметра конструктору Metafile. После создания объекта Metafile этот объект Metafile необходимо передать в качестве параметра методу DrawImage объекта Graphics.

В примере создается объект Metafile на основе файла в формате EMF (enhanced metafile — расширенный метафайл) и на экране рисуется соответствующее изображение с верхним левым углом в точке (60, 10).

На следующем рисунке показан метафайл, выведенный на экран в указанном месте.

--------

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

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

How to: Crop and Scale Images

The Graphics class provides several DrawImage methods, some of which have source and destination rectangle parameters that you can use to crop and scale images.

Example

The following example constructs an Image object from the disk file Apple.gif. The code draws the entire apple image in its original size. The code then calls the DrawImage method of a Graphics object to draw a portion of the apple image in a destination rectangle that is larger than the original apple image.

The DrawImage method determines which portion of the apple to draw by looking at the source rectangle, which is specified by the third, fourth, fifth, and sixth arguments. In this case, the apple is cropped to 75 percent of its width and 75 percent of its height.

The DrawImage method determines where to draw the cropped apple and how big to make the cropped apple by looking at the destination rectangle, which is specified by the second argument. In this case, the destination rectangle is 30 percent wider and 30 percent taller than the original image.

The following illustration shows the original apple and the scaled, cropped apple.

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

// Draw the image unaltered with its upper-left corner at (0, 0).

e.Graphics.DrawImage(image, 0, 0);

// Make the destination rectangle 30 percent wider and

// 30 percent taller than the original image.

// Put the upper-left corner of the destination

// rectangle at (150, 20).

int width = image.Width;

int height = image.Height;

RectangleF destinationRect = new RectangleF(

150,

20,

1.3f * width,

1.3f * height);

// Draw a portion of the image. Scale that portion of the image

// so that it fills the destination rectangle.

RectangleF sourceRect = new RectangleF(0, 0, .75f * width, .75f * height);

e.Graphics.DrawImage(

image,

destinationRect,

sourceRect,

GraphicsUnit.Pixel);

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. Make sure to replace Apple.gif with an image file name and path that are valid on your system.