清晰图片框-c#

清晰图片框-c#,c#,winforms,C#,Winforms,我想修改这个项目。 我想放置一个重置按钮,然后清除它创建的所有PictureBox数组 更可能的是,表单从一开始就是空的 这是它的一些代码 // Function to add PictureBox Controls private void AddControls(int cNumber) { imgArray = new System.Windows.Forms.PictureBox[cNumber]; // assign number arr

我想修改这个项目。 我想放置一个重置按钮,然后清除它创建的所有
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";
    }
//用于添加PictureBox控件的函数
专用void AddControls(int cNumber)
{
imgArray=new System.Windows.Forms.PictureBox[cNumber];//分配数字数组
对于(int i=0;i432)//一行中有六个图像
{
Xpos=8;//在左侧保留八个像素
Ypos=Ypos+72;//图像高度+8
}
Imgaray[i]。左=Xpos;
imgaray[i].Top=Ypos;
imgaray[i].宽度=64;
Imgaray[i].高度=64;
imgaray[i].Visible=true;
//用图像的名称和完整路径填充(标记)
imgaray[i].Tag=imgName[i];
imgArray[i].单击+=新建系统.EventHandler(单击图像);
this.BackPanel.Controls.Add(imgArray[i]);
Xpos=Xpos+72;//图像宽度+8
Application.DoEvents();
MyProgress.PerformStep();
打破
}
}
MyProgress.Visible=false;
}
私有bool ThumbnailCallback()
{
返回false;
}
私有void btnLoad_单击(对象发送方,System.EventArgs e)
{
ImagesInFolder();//加载图像
ShowFolderImages();//在PictureBox数组上显示图像
this.Text=“单击想要的图像”;
}
我该怎么做

很抱歉,我还没有重置按钮的任何代码。

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

您可以将图像设置为空:

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

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

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

祝你好运

我将按照以下步骤确保一切正常:

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;
    }  
} 
private void btnReset\u单击(对象发送者,System.EventArgs e)
{ 
对于(int x=this.BackPanel.Controls.Count-1;x>=0;x--)
{
if(this.BackPanel.Controls[x].GetType()==typeof(PictureBox))
此。背板。控件。移除(x);
}
对于(int x=0;x
如果您正在画框上绘图,并且要清除它:

Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);
之后,可以再次在控件上绘制。
我希望这能对其他人有所帮助。

您使用的是什么图形界面?WPF?Winforms?这与其说是一个C#问题,不如说是关于如何使用您正在使用的库。