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

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

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

How to: Create Thumbnail Images

A thumbnail image is a small version of an image. You can create a thumbnail image by calling the GetThumbnailImage method of an Image object.

Example

The following example constructs an Image object from the file Compass.bmp. The original image has a width of 640 pixels and a height of 479 pixels. The code creates a thumbnail image that has a width of 100 pixels and a height of 100 pixels.

The following illustration shows the thumbnail image.

Image image = new Bitmap("Compass.bmp");

Image pThumbnail = image.GetThumbnailImage(100, 100, null, new

IntPtr());

e.Graphics.DrawImage(

pThumbnail,

10,

10,

pThumbnail.Width,

pThumbnail.Height);

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.

Создание эскизов изображений

Эскиз изображения — это сильно уменьшенный вариант изображения. Чтобы создать эскиз изображения, следует вызвать метод GetThumbnailImage объекта Image.

Пример

В следующем примере показано создание объекта Image из файла Compass.bmp. Исходное изображение имеет ширину 640 точек и высоту 479 точек. Приведенный ниже код создает эскиз изображения, имеющий ширину 100 точек и высоту 100 точек.

Эскиз изображения показан на следующем рисунке.

-----

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

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

How to: Improve Performance by Avoiding Automatic Scaling

GDI+ may automatically scale an image as you draw it, which would decrease performance. Alternatively, you can control the scaling of the image by passing the dimensions of the destination rectangle to the DrawImage method.

For example, the following call to the DrawImage method specifies an upper-left corner of (50, 30) but does not specify a destination rectangle.

e.Graphics.DrawImage(image, 50, 30); // upper-left corner at (50, 30)

Although this is the easiest version of the DrawImage method in terms of the number of required arguments, it is not necessarily the most efficient. If the resolution used by GDI+ (usually 96 dots per inch) is different from the resolution stored in the Image object, then the DrawImage method will scale the image. For example, suppose an Image object has a width of 216 pixels and a stored horizontal resolution value of 72 dots per inch. Because 216/72 is 3, DrawImage will scale the image so that it has a width of 3 inches at a resolution of 96 dots per inch. That is, DrawImage will display an image that has a width of 96x3 = 288 pixels.

Even if your screen resolution is different from 96 dots per inch, GDI+ will probably scale the image as if the screen resolution were 96 dots per inch. That is because a GDI+ Graphics object is associated with a device context, and when GDI+ queries the device context for the screen resolution, the result is usually 96, regardless of the actual screen resolution. You can avoid automatic scaling by specifying the destination rectangle in the DrawImage method.