Очистить PictureBox - c # - PullRequest
       0

Очистить PictureBox - c #

3 голосов
/ 28 февраля 2012

Я бы хотел изменить этот PictureBox Array Project. я хочу поставить кнопку сброса, чтобы очистить все PictureBox Array он создал
Скорее всего, форма снова будет пустой, как с самого начала.

это часть кода;

        // 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 #.

Ответы [ 4 ]

5 голосов
/ 28 февраля 2012

Вы можете просто установить изображение на нуль:

private void Clear()
{   
   foreach (var pictureBox in imgArray)
   {
      pictureBox.Image = null;
      pictureBox.Invalidate();
   }
}
2 голосов
/ 28 февраля 2012

Я буду следовать этим шагам, чтобы быть уверенным, что все будет в порядке:

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 голосов
/ 25 января 2016

Если вы рисуете на картинке и хотите очистить его:

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

После этого вы можете снова рисовать на элементе управления.Я надеюсь, что это может кому-то помочь.

0 голосов
/ 28 февраля 2012

При условии, что в this.Backpanel нет других дочерних элементов управления (контейнерный элемент управления, который фактически отображает ваши изображения), это, вероятно, будет работать:

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

Удачи!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...