清除PictureBox - c#

3
我希望您能修改这个PictureBox数组项目。 我想要添加一个重置按钮,可以清除所有创建的PictureBox数组,使表单恢复为空白状态,就像一开始一样。
以下是部分代码;
        // 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#的新手。


你使用哪个图形界面?WPF?Winforms?这不仅仅是一个关于C#的问题,更多地是关于如何使用你正在使用的库。 - Almo
4个回答

7

您可以将图像设置为null:

private void Clear()
{   
   foreach (var pictureBox in imgArray)
   {
      pictureBox.Image = null;
      pictureBox.Invalidate();
   }
}

2
我会按照以下步骤确保一切都是正确的:
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;
    }  
} 

0

如果你正在pictureBox上绘图并且想要清除它:

Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);

之后您可以在控件上再次绘制。 希望这能对某人有所帮助。


0
假设在 this.Backpanel(实际显示您的图像的容器控件)中没有其他子控件,则以下代码可能有效:
private void ClearImages() {
   this.BackPanel.Controls.Clear();
   imgArray = null;
}

祝你好运!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接