英語で読む

次の方法で共有


ImageList.ImageCollection クラス

定義

ImageList 内の Image オブジェクトのコレクションをカプセル化します。

C#
public sealed class ImageList.ImageCollection : System.Collections.IList
継承
ImageList.ImageCollection
実装

次のコード例は、 クラスの ImageList プロパティ (型) からイメージをImages選択、削除、および表示する方法をImageList.ImageCollection示しています。

C#
namespace myImageRotator
{
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;
 
    public class Form1 : System.Windows.Forms.Form
    {
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ImageList imageList1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        protected Graphics myGraphics;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label5;
        private int currentImage = 0;
 
        public Form1()
        {
            InitializeComponent();
            imageList1 = new ImageList () ;

            // The default image size is 16 x 16, which sets up a larger
            // image size. 
            imageList1.ImageSize = new Size(255,255);
            imageList1.TransparentColor = Color.White;

            // Assigns the graphics object to use in the draw options.
            myGraphics = Graphics.FromHwnd(panel1.Handle);
        }
 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label5 = new System.Windows.Forms.Label();
            this.SuspendLayout();

            this.listBox1.Location = new System.Drawing.Point(16, 16);
            this.listBox1.Size = new System.Drawing.Size(400, 95);
            this.listBox1.TabIndex = 0;

            this.label3.Location = new System.Drawing.Point(24, 168);
            this.label3.Text = "label3";

            this.button1.Location = new System.Drawing.Point(96, 128);
            this.button1.Size = new System.Drawing.Size(104, 23);
            this.button1.Text = "Show Next Image";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.button2.Location = new System.Drawing.Point(208, 128);
            this.button2.Size = new System.Drawing.Size(104, 23);
            this.button2.Text = "Remove Image";
            this.button2.Click += new System.EventHandler(this.button2_Click);

            this.button3.Location = new System.Drawing.Point(320, 128);
            this.button3.Text = "Clear List";
            this.button3.Click += new System.EventHandler(this.button3_Click);

            this.button4.Location = new System.Drawing.Point(16, 128);
            this.button4.Text = "Open Image";
            this.button4.Click += new System.EventHandler(this.button4_Click);

            this.pictureBox1.Location = new System.Drawing.Point(328, 232);
            this.pictureBox1.Size = new System.Drawing.Size(336, 192);

            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;

            this.panel1.Location = new System.Drawing.Point(8, 240);
            this.panel1.Size = new System.Drawing.Size(296, 184);

            this.label5.Location = new System.Drawing.Point(168, 168);
            this.label5.Size = new System.Drawing.Size(312, 40);
            this.label5.Text = "label5";

            this.ClientSize = new System.Drawing.Size(672, 461);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.listBox1);
            this.ResumeLayout(false);
        }

        // Display the image.
        private void button1_Click (object sender, System.EventArgs e)
        {
            if(imageList1.Images.Empty != true)
            {
                if(imageList1.Images.Count-1 > currentImage)
                {
                    currentImage++;
                }
                else
                {
                    currentImage=0;
                }
                panel1.Refresh();
                
                // Draw the image in the panel.
                imageList1.Draw(myGraphics,10,10,currentImage);

                // Show the image in the PictureBox.
                pictureBox1.Image = imageList1.Images[currentImage];
                label3.Text = "Current image is " + currentImage ;
                listBox1.SelectedIndex = currentImage;
                label5.Text = "Image is " + listBox1.Text ;
            }
        }
 
        // Remove the image.
        private void button2_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.RemoveAt(listBox1.SelectedIndex);
            listBox1.Items.Remove(listBox1.SelectedItem);
        }
 
        // Clear all images.
        private void button3_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.Clear();
            listBox1.Items.Clear();
        }
 
        // Find an image.
        private void button4_Click (object sender, System.EventArgs e)
        {
            openFileDialog1.Multiselect = true ;
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileNames != null)
                {
                    for(int i =0 ; i < openFileDialog1.FileNames.Length ; i++ )
                    {
                        addImage(openFileDialog1.FileNames[i]);
                    }
                }
                else
                {
                    addImage(openFileDialog1.FileName);
                }
            }
        }
 
        private void addImage(string imageToLoad)
        {
            if (imageToLoad != "")
            {
                imageList1.Images.Add(Image.FromFile(imageToLoad));
                listBox1.BeginUpdate();
                listBox1.Items.Add(imageToLoad);
                listBox1.EndUpdate();
            }
        }
        [STAThread]
        public static void Main(string[] args) 
        {
            Application.Run(new Form1());
        }
    }
}

注釈

これは、 のイメージをプログラムで ImageList 管理するために使用され、イメージ オブジェクトを追加および削除するメソッドを提供します。

プロパティ

Count

現在リスト内にあるイメージの数を取得します。

Empty

ImageList 内にイメージがあるかどうかを示す値を取得します。

IsReadOnly

リストが読み取り専用かどうかを示す値を取得します。

Item[Int32]

コレクション内の指定したインデックスにある Image を取得または設定します。

Item[String]

指定したキーを持つ Image をコレクションから取得します。

Keys

ImageList.ImageCollection 内のイメージに関連付けられたキーのコレクションを取得します。

メソッド

Add(Icon)

指定したアイコンを ImageList に追加します。

Add(Image)

指定したイメージを ImageList に追加します。

Add(Image, Color)

指定した色を使用してマスクを生成し、指定したイメージを ImageList に追加します。

Add(String, Icon)

指定したキーのアイコンをコレクションの末尾に追加します。

Add(String, Image)

指定したキーのイメージをコレクションの末尾に追加します。

AddRange(Image[])

コレクションにイメージの配列を追加します。

AddStrip(Image)

指定したイメージのイメージ ストリップを ImageList に追加します。

Clear()

ImageList からすべてのイメージとマスクを削除します。

Contains(Image)

サポートされていません。 Contains(Object) メソッドは、指定したオブジェクトがリストに含まれているかどうかを示します。

ContainsKey(String)

指定したキーを持つイメージがコレクションに格納されているかどうかを確認します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

項目のコレクションを反復処理するために使用できる列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(Image)

サポートされていません。 IndexOf(Object) メソッドは、リスト内の指定したオブジェクトのインデックスを返します。

IndexOfKey(String)

指定されたキーを持つイメージがコレクション内で最初に見つかったときのインデックスを判断します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(Image)

サポートされていません。 Remove(Object) メソッドは、指定したオブジェクトをリストから削除します。

RemoveAt(Int32)

イメージをリストから削除します。

RemoveByKey(String)

指定したキーを持つイメージをコレクションから削除します。

SetKeyName(Int32, String)

コレクション内のイメージのキーを設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

コピー先配列の指定されたインデックスを開始位置として、このコレクション内の項目を互換性がある 1 次元配列にコピーします。

ICollection.IsSynchronized

コレクションへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

ICollection.SyncRoot

コレクションへのアクセスを同期するために使用できるオブジェクトを取得します。

IList.Add(Object)

指定したイメージを ImageList に追加します。

IList.Contains(Object)

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

Contains(Object) メソッドを実装します。 常に NotSupportedException をスローします。

IList.IndexOf(Object)

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

IndexOf(Object) メソッドを実装します。 常に NotSupportedException をスローします。

IList.Insert(Int32, Object)

Insert(Int32, Object) メソッドを実装します。 常に NotSupportedException をスローします。

IList.IsFixedSize

ImageList.ImageCollection が固定サイズかどうかを示す値を取得します。

IList.Item[Int32]

既存の ImageList.ImageCollection 内のイメージを取得または設定します。

IList.Remove(Object)

Remove(Object) を実装します。 常に NotSupportedException をスローします。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9