+92 332 4229 857 99ProjectIdeas@Gmail.com

Searching All Wallpapers In Computer (C#.net)





The following code shows you finding all (.jpg) wallpapers in your computer.







Thread threadPictureBox;

int flag = 0;

//Searching one by one every drive

public void getFolders()
{
     DriveInfo[] dinfo = DriveInfo.GetDrives();
     foreach (DriveInfo d in dinfo)
         if(d.IsReady)
            getAllWallpaers(d.Name);
}

//Searching wallpaper function
public void getAllWallpaers(string parentFolder)
{
   try
       {

        foreach (string childFolder in Directory.GetDirectories(parentFolder))
        {
              getAllWallpaers(childFolder);// a recursive call
        }

        foreach (String wallpapers in Directory.GetFiles(parentFolder, "*.jpg", SearchOption.AllDirectories))
        {
              Image img = Image.FromFile(wallpapers);
              label7.Text = wallpapers;//Path where the picture is located
              Image targetImage = fitInPictureBox(img, img.Size);
              pictureBox1.Image = targetImage;
        }
      }
       catch (Exception ex)
      {
           
      }
}

public Image fitInPictureBox(Image imgToResize, Size imgSize)
{
   int newWidth = pictureBox1.Size.Width;
   int newHeight = pictureBox1.Size.Height;

   imgSize.Width = newWidth;
   imgSize.Height = newHeight;

   Bitmap resizedImage = new Bitmap(imgToResize, new Size(newWidth, newHeight));

   return (Image)resizedImage;
}


public void btnSearch_Click(object sender, EventArgs e)
{
  try
      {
         btnSearch.Text = "Stop";

           if (flag == 0)
           {
                threadPictureBox = new Thread(getFolders);
                threadPictureBox.Start();
                flag = 1;
           }
           else if (flag == 1)
           {
                btnSearch.Text = "Resume";
                threadPictureBox.Suspend();
                flag = 2;
           }
            else if (flag == 2)
           {
                threadPictureBox.Resume();
                flag = 1;
           }
       }
        catch (Exception ex)
       {
            MessageBox.Show(ex.Message);
       }
}


0 comments: