C#中ImageList组件应用实例
ImageList组件
1.功能
ImageList组件用于存储图像,这些图像可由控件显示。图像列表能够为一致的单个图像目录编写代码。图1所示为ImageList组件。

图1 ImageList组件
2.属性
ImageList组件常用属性及说明如表1所示。

表1 ImageList组件常用属性及说明
下面详细介绍Images属性,此属性表示图像列表中包含图像的集合。
语法:
public ImageCollection Images { get; }
属性值:图像集合。
示例
Images属性的使用
本示例主要是通过Images属性为pictureBox1控件的Image属性赋值。
程序主要代码如下:
this.pictureBox1.Image = imageList1.Images[0];
this.pictureBox2.Image = imageList1.Images[1];
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_11
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmPictureBox());
}
}
}
★★★★★frmPictureBox窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_11
{
public partial class frmPictureBox : Form
{
public frmPictureBox()
{
InitializeComponent();
}
private void frmPictureBox_Load(object sender, EventArgs e)
{
this.pictureBox1.Image = imageList1.Images[0];
this.pictureBox2.Image = imageList1.Images[1];
this.pictureBox1.SizeMode= PictureBoxSizeMode.StretchImage;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
}
}
★★★★★frmPictureBox窗体代码文件完整程序代码★★★★★
namespace _8_11
{
partial class frmPictureBox
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmPictureBox));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(70, 52);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("黑体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(24, 179);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 19);
this.label2.TabIndex = 5;
//
// pictureBox2
//
this.pictureBox2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox2.Location = new System.Drawing.Point(139, 33);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(141, 97);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox2.TabIndex = 4;
this.pictureBox2.TabStop = false;
this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(28, 33);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(85, 87);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "Auto.jpg");
this.imageList1.Images.SetKeyName(1, "2005122314034518.jpg");
this.imageList1.Images.SetKeyName(2, "11.bmp");
//
// frmPictureBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(296, 238);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Name = "frmPictureBox";
this.Text = "ImageList组件";
this.Load += new System.EventHandler(this.frmPictureBox_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ImageList imageList1;
}
}
3.方法