首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除PictureBox - c#

清除PictureBox - c#
EN

Stack Overflow用户
提问于 2012-02-28 06:41:24
回答 4查看 14.5K关注 0票数 3

我想修改这个PictureBox Array项目。我想放一个重置按钮,这样就可以清除它创建的所有PictureBox Array

更有可能的是,表单将再次为空,就像从头开始一样。

这是它的一些代码;

代码语言:javascript
复制
        // Function to add PictureBox Controls
    private void AddControls(int cNumber)
    {
        imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number array 
        for (int i = 0; i < cNumber; i++)
        {
            imgArray[i] = new System.Windows.Forms.PictureBox(); // Initialize one variable
        }
        // When call this function you determine number of controls
    }  

    private void ImagesInFolder()
    {
        FileInfo FInfo;
        // Fill the array (imgName) with all images in any folder 
        imgName = Directory.GetFiles(Application.StartupPath  + @"\Images");
        // How many Picture files in this folder
        NumOfFiles = imgName.Length; 
        imgExtension = new string[NumOfFiles];
        for (int i = 0; i < NumOfFiles; i++)
        {
            FInfo = new FileInfo(imgName[i]);
            imgExtension[i] = FInfo.Extension; // We need to know the Extension
            //
        }
    }

    private void ShowFolderImages()
    {
        int Xpos = 8; 
        int Ypos = 8;
        Image img;
        Image.GetThumbnailImageAbort myCallback = 
            new Image.GetThumbnailImageAbort(ThumbnailCallback);
        MyProgress.Visible = true;
        MyProgress.Minimum = 1;
        MyProgress.Maximum = NumOfFiles;
        MyProgress.Value = 1;
        MyProgress.Step = 1; 
        string[] Ext = new string [] {".GIF", ".JPG", ".BMP", ".PNG"};
        AddControls(NumOfFiles);
        for (int i = 0; i < NumOfFiles; i++)
        {
            switch (imgExtension[i].ToUpper())
            {
                case ".JPG":
                case ".BMP":
                case ".GIF":
                case ".PNG":
                    img = Image.FromFile(imgName[i]); // or img = new Bitmap(imgName[i]);
                    imgArray[i].Image = img.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero);
                    img = null;
                    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;
                    // Fill the (Tag) with name and full path of image
                    imgArray[i].Tag = imgName[i]; 
                    imgArray[i].Click += new System.EventHandler(ClickImage);
                    this.BackPanel.Controls.Add(imgArray[i]);
                    Xpos = Xpos + 72; // width of image + 8
                    Application.DoEvents();
                    MyProgress.PerformStep();
                    break;
            }
        }
        MyProgress.Visible = false;
    }

    private bool ThumbnailCallback()
    {
        return false;
    }

    private void btnLoad_Click(object sender, System.EventArgs e)
    {
        ImagesInFolder(); // Load images
        ShowFolderImages(); // Show images on PictureBox array 
        this.Text = "Click wanted image";
    }

我该怎么做呢?

对不起,我还没有重置按钮的任何代码。

我不知道该怎么做,我是c#的新手。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-28 07:10:11

我将遵循以下步骤,以确保一切都是正确的:

代码语言:javascript
复制
private void btnReset_Click(object sender, System.EventArgs e) 
{ 
    for(int x = this.BackPanel.Controls.Count - 1; x >= 0; x--)
    {
        if(this.BackPanel.Controls[x].GetType() == typeof(PictureBox))
            this.BackPanel.Controls.Remove(x);
    }

    for(int x = 0; x < imgArray.Length; x++)
    {
        imgArray[x].Image = null;  
        imgArray[x] = null;
    }  
} 
票数 2
EN

Stack Overflow用户

发布于 2012-02-28 07:00:51

您可以只将图像设置为null:

代码语言:javascript
复制
private void Clear()
{   
   foreach (var pictureBox in imgArray)
   {
      pictureBox.Image = null;
      pictureBox.Invalidate();
   }
}
票数 7
EN

Stack Overflow用户

发布于 2012-02-28 07:09:08

假设this.Backpanel中没有其他子控件(实际显示图像的容器控件),这可能会起作用:

代码语言:javascript
复制
private void ClearImages() {
   this.BackPanel.Controls.Clear();
   imgArray = null;
}

祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9473530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档