Home > Articles > Home & Office Computing

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Searching

Let's look at how to perform the search. The user interface for the application is very simple. It has a button that simply says Retrieve My Pictures (see Figure 2). Clicking the Retrieve My Pictures button opens a dialog box to ask the user where to download everything (see Figure 3).

Figure 2

Figure 2 Two-button interface for Flickr Archivr.

Figure 3

Figure 3 Specifying where to export.

On the main form, the download button is called btnSearch and it will perform the search for all the user's photos and perform the file download. Here's the entire Click method for that button:

private void btnSearch_Click(object sender, System.EventArgs e)
{
  System.IO.Stream inputStream = null;
  System.IO.BufferedStream bufferedInput = null;
  System.IO.FileStream fileOutputStream = null;
  FolderBrowserDialog browseDialog = new FolderBrowserDialog();

  int len = 2048;
  Byte[] buff = new Byte[len];
  int read = -1;
  String outputPath = "";

  browseDialog.Description = "Select where to output your images";

  //Display the dialog and exit if cancel was pressed
  if(browseDialog.ShowDialog(this) != DialogResult.OK)
  {  return; }

  //Iterate over all files returned in the search
  foreach(Photo photo in
    flickr.PhotosSearch(user.UserId, "").PhotoCollection)
  {
    inputStream =
      flickr.DownloadPicture(photo.LargeUrl.ToString());
    bufferedInput = new System.IO.BufferedStream(inputStream);
    outputPath = browseDialog.SelectedPath +
      "\\" + photo.Title + ".jpg";
    fileOutputStream = new System.IO.FileStream(outputPath,
      System.IO.FileMode.Create);

    while((read = bufferedInput.Read(buff, 0, len)) > 0)
    {  fileOutputStream.Write(buff, 0, read); }

    bufferedInput.Close();
    fileOutputStream.Close();
  }
}

There are quite a few things going on here. First, we'll need a variety of objects from System.IO to handle the actual downloading of the file. The Flickr.NET API kindly gives us a Stream from a URL, but we have to handle the process of translating that stream to a file. For the user to select where to save the files, an instance of FolderBrowserDialog is required. Finally, there's the searching itself, performed through our instance of Flickr. The code that actually performs the search is this:

flickr.PhotosSearch(user.UserId, "").PhotoCollection

This line gives us a Collection of Photo objects. The first parameter to the PhotosSearch function is the ID of the user (which is not the same as the username, by the way) and the second allows you to additionally search within the photos tags.

Once you have that Collection, it's easy enough to iterate through it and, using some fairly simple file I/O, write the individual photos to the directory the user specified, using the Title of the Photo to provide the filename. This code will overwrite any file that already exists, without prompting.

  • + Share This
  • 🔖 Save To Your Account