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

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

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

How to: Create a Bitmap at Run Time

This example creates and draws in a Bitmap object and displays it in an existing Windows Forms PictureBox control.

Example

PictureBox pictureBox1 = new PictureBox();

public void CreateBitmapAtRuntime()

{

pictureBox1.Size = new Size(210, 110);

this.Controls.Add(pictureBox1);

Bitmap flag = new Bitmap(200, 100);

Graphics flagGraphics = Graphics.FromImage(flag);

int red = 0;

int white = 11;

while (white <= 100) {

flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);

flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);

red += 20;

white += 20;

}

pictureBox1.Image = flag;

}

Compiling the Code

This example requires:

  • A Windows Form that imports the System, System.Drawing and System.Windows.Forms assemblies.

Создание растрового изображения во время выполнения

В этом примере демонстрируется создание объекта Bitmap и рисование в нем, после чего объект отображается на существующей форме Windows Forms в элементе управления PictureBox.

Пример

-----

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

Для этого примера требуются следующие компоненты.

  • Форма Windows Form, импортирующая сборки System, System.Drawing и System.Windows.Forms.

How to: Extract the Icon Associated with a File in Windows Forms

Many files have embedded icons that provide a visual representation of the associated file type. For example, Microsoft Word documents contain an icon that identifies them as Word documents. When displaying files in a list control or table control, you may want to display the icon representing the file type next to each file name. You can do this easily by using the ExtractAssociatedIcon method.

Example

The following code example demonstrates how to extract the icon associated with a file and display the file name and its associated icon in a ListView control.

ListView listView1;

ImageList imageList1;

public void ExtractAssociatedIconEx()

{

// Initialize the ListView, ImageList and Form.

listView1 = new ListView();

imageList1 = new ImageList();

listView1.Location = new Point(37, 12);

listView1.Size = new Size(151, 262);

listView1.SmallImageList = imageList1;

listView1.View = View.SmallIcon;

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.listView1);

this.Text = "Form1";

// Get the c:\ directory.

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\");

ListViewItem item;

listView1.BeginUpdate();