画像を分割して登録

16枚の画像を切り分けて Imagelist に登録します。
次にマウスのクリックで切り替えながらアニメーションします。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. Image List は登録した画像を、インデックス(登録番号)で呼び出す Object Class です。
    主にツールバーやリストビューなどにアイコンを張り付けるときに使用します。
    今回は大きな画像を切り分けて Image List に登録してみました。
    Bitmap bijin; が 60ピクセル*60ピクセルの画像が4行4列に並んだ元の画像です。
    ページ先頭の画像(bijin16.jpg) を c:\data\test\ に格納しておいて下さい。
    Bitmap[] sp_bijin; が切り分けた画像を格納する Bitmap の配列です。
    画像の分割は 画像を切り分ける を参照して下さい。
        Bitmap      bijin;
        Bitmap[]    sp_bijin = new Bitmap[16];
    
  2. C言語(Windows)では、Image List 自体に画像を切り分ける機能があるのですが C# には見当たりません。
    仕方が無いので、Graphics を取得して一枚ずつ切り分けて登録します。
            for(int i=0; i<16; i++)
            {   sp_bijin[i] = new Bitmap(60,60);
                graphics = Graphics.FromImage(sp_bijin[i]);
                graphics.DrawImage(bijin,new Rectangle(0,0,60,60),
                    new Rectangle(60*(i%4),60*(i/4),60,60),GraphicsUnit.Pixel);
                imglist.Images.Add(sp_bijin[i]);
            }
        
    C言語の Image List は 画像を5枚に分割して描画する を参照して下さい。
  3. 16枚の画像の登録が終われば、マウスのクリックで順番に切り替えながらアニメーションします。
        private void OnMyMouseDown(object sender, MouseEventArgs e)
        {
            SP_NO= (SP_NO+1)%16;
            Invalidate();
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            imglist.Draw(e.Graphics, 60, 60, SP_NO);
        }
    
  4. 全ソースコードです。
    完成したプログラムなので Command Line から Windows プログラムを実行 でもOKです。
    /**************************************************/
    /*★ 画像を分割して ImageList に登録    前田 稔 ★*/
    /**************************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    class MyForm : Form
    {
        Bitmap      bijin;
        ImageList   imglist;
        Bitmap[]    sp_bijin = new Bitmap[16];
        Graphics    graphics;
        int         SP_NO;
    
        public MyForm()
        {
            try
            {   bijin = new Bitmap("c:\\data\\test\\bijin16.jpg");  }
            catch
            {   MessageBox.Show("画像ファイルが読めません!");
                return ;
            }
            imglist = new ImageList();
            imglist.ImageSize = new Size(60, 60);
     
            for(int i=0; i<16; i++)
            {   sp_bijin[i] = new Bitmap(60,60);
                graphics = Graphics.FromImage(sp_bijin[i]);
                graphics.DrawImage(bijin,new Rectangle(0,0,60,60),
                    new Rectangle(60*(i%4),60*(i/4),60,60),GraphicsUnit.Pixel);
                imglist.Images.Add(sp_bijin[i]);
            }
            Paint += new PaintEventHandler(MyHandler);
            MouseDown += new MouseEventHandler(OnMyMouseDown);
            SP_NO = 0;
        }
    
        private void MyHandler(object sender, PaintEventArgs e)
        {
            imglist.Draw(e.Graphics, 60, 60, SP_NO);
        }
    
        private void OnMyMouseDown(object sender, MouseEventArgs e)
        {
            SP_NO= (SP_NO+1)%16;
            Invalidate();
        }
    }
    
    class form01
    {   public static void Main()
        {   MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

超初心者のプログラム入門(C# Frame Work)