简体   繁体   English

清除PictureBox - c#

[英]Clear PictureBox - c#

I'd like to modify this PictureBox Array Project. 我想修改这个PictureBox数组项目。 i want to put a reset button than will clear all the PictureBox Array it created 我想放一个重置按钮,而不是清除它创建的所有PictureBox Array
more likely the form will be empty again as like from the beginning. 更可能的形式将从头开始再次为空。

this is some of it's code; 这是它的一些代码;

        // 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";
    }

how can i do that? 我怎样才能做到这一点?

sorry i don't have any code for the reset button yet. 抱歉,我还没有重置按钮的任何代码。
i don't know what to do, i am new to c#. 我不知道该怎么做,我是c#的新手。

You can just set the image to null: 您只需将图像设置为null:

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

I will follow this steps to be sure everything will be fred : 我将按照这些步骤确保一切都将成为现实:

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

If you are drawing on a pictureBox and you want to clear it: 如果您正在绘制一个pictureBox并且想要清除它:

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

After that you can draw again on the control. 之后,您可以再次在控件上绘制。 I hope this can help to someone. 我希望这对某人有帮助。

Assuming there are no other child controls in this.Backpanel (the container control that is actually displaying your images), this will probably work: 假设this.Backpanel (实际显示图像的容器控件)中没有其他子控件,这可能会起作用:

private void ClearImages() {
   this.BackPanel.Controls.Clear();
   imgArray = null;
}

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM