次の方法で共有


ImageList クラス

Image オブジェクトのコレクションを管理するメソッドを提供します。このクラスは継承できません。

この型のすべてのメンバの一覧については、ImageList メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.ImageList

NotInheritable Public Class ImageList
   Inherits Component
[C#]
public sealed class ImageList : Component
[C++]
public __gc __sealed class ImageList : public Component
[JScript]
public class ImageList extends Component

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ImageList は、一般的に ListViewTreeViewToolBar などのほかコントロールで使用されます。 ImageList にはビットマップ、アイコン、またはメタファイルを追加でき、その他のコントロールは必要に応じてそのイメージを使用できます。

ImageList は、イメージのリストを管理するハンドルを使用します。 Count の取得、 Handle の取得、 Draw の呼び出しなど、特定の操作がイメージ リストで実行されるまで Handle は作成されません。

使用例

[Visual Basic, C#, C++] イメージの選択、イメージの削除、およびイメージの表示のコード例を次に示します。

 
Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace myImageRotator    
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Protected components As Container
        Protected listBox1 As ListBox
        Protected label2 As Label
        Protected label3 As Label
        Protected label5 As Label
        Protected pictureBox1 As PictureBox
        Protected button1 As Button
        Protected button2 As Button
        Protected button3 As Button
        Protected button4 As Button
        Protected panel1 As Panel
        Protected imageList1 As ImageList
        Protected myGraphics As Graphics
        Protected openFileDialog1 As OpenFileDialog
        
        Private currentImage As Integer = 0        
        
        Public Sub New()
            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)
        End Sub        
        

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
           If disposing Then
               If Not (components Is Nothing) Then
                   components.Dispose()
               End If
           End If
           MyBase.Dispose(disposing)
        End Sub

       
        
        Private Sub InitializeComponent()
            ' Initializations for listBox1, label2, pictureBox1,
            ' button2, button3, panel1, openFileDialog1, button4, label1, 
            ' button1, and imageList1.
        End Sub
        
        Protected Sub button1_Click(sender As Object, e As System.EventArgs)
            displayNextImage()
        End Sub        
        
        Protected Sub button2_Click(sender As Object, e As System.EventArgs)
            imageList1.Images.RemoveAt(listBox1.SelectedIndex)
            listBox1.Items.Remove(listBox1.SelectedIndex)
        End Sub        
        
        Protected Sub button3_Click(sender As Object, e As System.EventArgs)
            imageList1.Images.Clear()
        End Sub        
        
        Protected Sub button4_Click(sender As Object, e As System.EventArgs)
            openFileDialog1.Multiselect = True
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                If Not (openFileDialog1.FileNames Is Nothing) Then
                    Dim i As Integer
                    For i = 0 To openFileDialog1.FileNames.Length - 1
                        addImage(openFileDialog1.FileNames(i))
                    Next i
                Else
                    addImage(openFileDialog1.FileName)
                End If
            End If
        End Sub
         
        Private Sub addImage(imageToLoad As String)
            If imageToLoad <> "" Then
                imageList1.Images.Add(Image.FromFile(imageToLoad))
                listBox1.BeginUpdate()
                listBox1.Items.Add(imageToLoad)
                listBox1.EndUpdate()
            End If
        End Sub        
        
        Sub displayNextImage()
            If imageList1.Images.Empty <> True Then
                If imageList1.Images.Count - 1 < currentImage Then
                    currentImage += 1
                Else
                    currentImage = 0
                End If
                panel1.Refresh()
                imageList1.Draw(myGraphics, 10, 10, currentImage)
                pictureBox1.Image = imageList1.Images(currentImage)
                label3.Text = "Current image is " & currentImage
                listBox1.SelectedIndex = currentImage
                label5.Text = "Image is " & listBox1.Text
            End If
        End Sub
        
        Public Shared Sub Main()
            Application.Run(New Form1())
        End Sub
        
    End Class
    
End Namespace


[C#] 
namespace myImageRotator
{
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class Form1 : System.Windows.Forms.Form
    {
        protected Container components;
        protected ListBox listBox1;
        protected Label label2;
        protected Label label3;
        protected Label label5;
        protected PictureBox pictureBox1;
        protected Button button1;
        protected Button button2;
        protected Button button3;
        protected Button button4;
        protected Panel panel1;
        protected ImageList imageList1;
        protected Graphics myGraphics;
        protected OpenFileDialog openFileDialog1;

        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);
        }

       protected override void Dispose( bool disposing )
       {
           if( disposing )
           {
               if (components != null) 
               {
                   components.Dispose();
               }
           }
           base.Dispose( disposing );
       }

        private void InitializeComponent()
        {
            // Initializations for listBox1, label2, pictureBox1,
            // button2, button3, panel1, openFileDialog1, button4, label1, 
            // button1, and imageList1.
        }

        protected void button1_Click (object sender, System.EventArgs e)
        {
            displayNextImage();
        }

        protected void button2_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.RemoveAt(listBox1.SelectedIndex);
            listBox1.Items.Remove(listBox1.SelectedIndex);
        }

        protected void button3_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.Clear();
        }

        protected 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();
            }
        }

        void displayNextImage()
        {
            if(imageList1.Images.Empty != true)
            {
                if(imageList1.Images.Count-1 < currentImage)
                {
                    currentImage++;
                }
                else
                    currentImage=0;
                panel1.Refresh();
                imageList1.Draw(myGraphics,10,10,currentImage);
                pictureBox1.Image = imageList1.Images[currentImage];
                label3.Text = "Current image is " + currentImage ;
                listBox1.SelectedIndex = currentImage;
                label5.Text = "Image is " + listBox1.Text ;
            }            
        }

        public static void Main(string[] args) 
        {
            Application.Run(new Form1());
        }
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

public __gc class Form1 : public System::Windows::Forms::Form
{
protected:
   System::ComponentModel::Container* components;
   ListBox* listBox1;
   Label* label2;
   Label* label3;
   Label* label5;
   PictureBox* pictureBox1;
   Button* button1;
   Button* button2;
   Button* button3;
   Button* button4;
   Panel* panel1;
   ImageList* imageList1;
   Graphics* myGraphics;
   OpenFileDialog* openFileDialog1;

private:
   int currentImage;

public:
   Form1()
   {
      currentImage = 0;
      InitializeComponent();
      imageList1 = new ImageList () ;
      // The default image size is 16 x 16, which sets up a larger
      // image size. 
      imageList1->ImageSize = System::Drawing::Size(255,255);
      imageList1->TransparentColor = Color::White;
      // Assigns the graphics object to use in the draw options.
      myGraphics = Graphics::FromHwnd(panel1->Handle);
   }

protected:
   void Dispose( bool disposing )
   {
      if( disposing )
      {
         if (components != 0) 
         {
            components->Dispose();
         }
      }
      Form::Dispose( disposing );
   }

private:
   void InitializeComponent()
   {
      // Initializations for listBox1, label2, pictureBox1,
      // button2, button3, panel1, openFileDialog1, button4, label1, 
      // button1, and imageList1.
   }

protected:
   void button1_Click (Object* /*sender*/, System::EventArgs* /*e*/)
   {
      displayNextImage();
   }

   void button2_Click (Object* /*sender*/, System::EventArgs* /*e*/)
   {
      imageList1->Images->RemoveAt(listBox1->SelectedIndex);
      listBox1->Items->Remove(__box(listBox1->SelectedIndex));
   }

   void button3_Click (Object* /*sender*/, System::EventArgs* /*e*/)
   {
      imageList1->Images->Clear();
   }

   void button4_Click (Object* /*sender*/, System::EventArgs* /*e*/)
   {
      openFileDialog1->Multiselect = true ;
      if(openFileDialog1->ShowDialog() == DialogResult::OK)
      {
         if (openFileDialog1->FileNames != 0)
         {
            for(int i =0 ; i < openFileDialog1->FileNames->Length ; i++ )
            {
               addImage(openFileDialog1->FileNames[i]);
            }
         }
         else
            addImage(openFileDialog1->FileName);
      }
   }

private:
   void addImage(String* imageToLoad)
   {
      if (!imageToLoad->Equals(S""))
      {
         imageList1->Images->Add(Image::FromFile(imageToLoad));
         listBox1->BeginUpdate();
         listBox1->Items->Add(imageToLoad);
         listBox1->EndUpdate();
      }
   }

   void displayNextImage()
   {
      if(imageList1->Images->Empty != true)
      {
         if(imageList1->Images->Count-1 < currentImage)
         {
            currentImage++;
         }
         else
            currentImage=0;
         panel1->Refresh();
         imageList1->Draw(myGraphics,10,10,currentImage);
         pictureBox1->Image = imageList1->Images->Item[currentImage];
         label3->Text = String::Format( S"Current image is {0}", __box(currentImage)) ;
         listBox1->SelectedIndex = currentImage;
         label5->Text = String::Format( S"Image is {0}", listBox1->Text ) ;
      }            
   }
};

int main() 
{
   Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

ImageList メンバ | System.Windows.Forms 名前空間 | Bitmap | Icon | Metafile