代码之家  ›  专栏  ›  技术社区  ›  JayJay

用images.winforms填充面板

  •  0
  • JayJay  · 技术社区  · 16 年前

    我将在带有图片框的面板图像中显示以下代码:

    private void ARR(int cNumber, string exc)
        {
           int Xpos = 8;
            int Ypos = 8;
            Image img;
                Image.GetThumbnailImageAbort myCallback = 
                new Image.GetThumbnailImageAbort(ThumbnailCallback);
            imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array 
            for (int i = 0; i < cNumber; i++)
            {
                imgArray[i] = new System.Windows.Forms.PictureBox(); 
                if (Xpos > 432) // six images in a line
                {
                    Xpos = 8; // leave eight pixels at Left 
                    Ypos = Ypos + 72;  // height of image + 8
                }     
                imgArray[i].Left = Xpos;
                imgArray[i].Top = Ypos;
                imgArray[i].Width = 64;
                imgArray[i].Height = 64;
                imgArray[i].Visible = true;         
                imgArray[i].SizeMode = PictureBoxSizeMode.StretchImage;
                img = Image.FromFile(exc);
                imgArray[i].Tag = exc[i]; 
                imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                panel1.Controls.Add(imgArray[i]);
                Xpos = Xpos + 72;
            }
    
        }
    

    私有列表GetPicture4(字符串文件夹) {

            DirectoryInfo dir = new DirectoryInfo(Folder);
            List<string> str = new List<string>();
            FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);   
            NumOfFiles = files.Length;
            imgExtension = new string[NumOfFiles];
    
            for (int i = 0; i < NumOfFiles; i++)
            {
    
            foreach (FileInfo file in files)
            {
                ARR(NumOfFiles, file.FullName);
                str.Add(file.FullName);
    
          }
            return str;
        }
    

    在“music”文件夹中有30个file.jpg,但当我调试时,面板显示30个图片,但相同的文件是“jpg”。 我不明白错误在哪里:(。 你知道我哪里错了吗? 非常感谢。

    亲切问候

    1 回复  |  直到 16 年前
        1
  •  1
  •   RvdK    16 年前

    尚未解决,但您的代码无法工作:

    private List<string> GetPicture4(string Folder)  //you need to define the type of list you are returning
    {
        DirectoryInfo dir = new DirectoryInfo(Folder);
        List<string> str = new List<string>();
        FileInfo[] files = dir.GetFiles("*.jpg", SearchOption.AllDirectories);   
        int NumOfFiles = files.Length; //here you are missing a type, int in this case
        imgExtension = new string[NumOfFiles];
    
        for (int i = 0; i < NumOfFiles; i++)
        {
            ARR(i, files[i].FullName); //pass i instead of NumOfFiles else in ARR the creating of the picturebox gets the same ID everytime
            str.Add(files[i].FullName);
        }
    
        return str;
    }
    

    编辑: 添加功能怎么样:

    imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array 
    

    这条线不应该在这里。您已经在循环中创建了一个

    推荐文章