Facebook allows its users to create multiple photo albums and upload up to 60 photos into each. It also allows you to add tags to images to show which of your friends are present in the image. There are some operations that cannot be performed through the Facebook API—you cannot delete or modify albums or photos. When you add a new photo it is invisible to other users. You must activate the picture from the Facebook Web site before it shows normally in your albums. The sample application for this section is a tool to browse and manage your own photo albums.
The main form of the application uses a ListView control to display a thumbnail image of each album. Figure 8 shows the opening form with a set of albums.
Figure 8. Facebook Photos
When the user selects a specific album, we then open up a new form showing all the photos in that album. From the main form you can also add a new album and view the properties of the currently selected album—Name, Location, and Description. Tapping New Album opens a new instance of the AlbumPropertiesForm where you can enter album information. When the form is closed (by tapping OK) the API is called to create a new album:
private void AlbumPropertiesForm_Closing(object sender, CancelEventArgs e)
{
if (isnew)
{
if(!string.IsNullOrEmpty(txtName.Text))
{
Album newAlbum = parent.facebookService1.CreateAlbum(
txtName.Text, txtLocation.Text, txtDescription.Text);
}
}
} When the user returns to the main form, we refresh the album listing. When an album is created it is instantly visible in the user’s album collection, even if it contains no images.
When an album is selected we open a new instance of AlbumForm passing the album details. The AlbumForm is designed in a similar way to the main form, for simplicity we are using the built-in ListView control. The photos are loaded in the Load event handler for the form:
private void AlbumForm_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
lvAlbum.BeginUpdate();
Collection<Photo> photos =
parentForm.facebookService1.GetPhotos(album.AlbumId);
int i = 0;
foreach (Photo p in photos)
{
ListViewItem lvi = new ListViewItem(p.Caption);
lvi.ImageIndex = i;
ilAlbum.Images.Add(p.PictureSmall);
lvAlbum.Items.Add(lvi);
i++;
}
lvAlbum.EndUpdate();
Cursor.Current = Cursors.Default;
}
Again the aspect ratio of the pictures can vary and so the presentation here with a ListView and ImageList results in some distorted images. However it provides a quick rendition of the album contents. Figure 9 shows the appearance of a typical album.
Figure 9. Album form
There is one menu item on this form that allows you to upload a new image to the album. Unlike creation of a new album, a newly uploaded photo remains invisible until you authorize it from the Facebook Web site. When you view the album on the Web site it will show a pending items listing for those awaiting approval. You cannot approve images from the mobile version of the Web site. To integrate capture of the image we use the CameraCaptureDialog from the Microsoft.WindowsMobile.Forms assembly.
private void mnuPhoto_Click(object sender, EventArgs e)
{
Microsoft.WindowsMobile.Forms.CameraCaptureDialog ccd =
new Microsoft.WindowsMobile.Forms.CameraCaptureDialog();
ccd.Mode = Microsoft.WindowsMobile.Forms.CameraCaptureMode.Still;
ccd.Title = "Add photo to Facebook";
ccd.Resolution = new Size(640, 480);
//if success
if (ccd.ShowDialog() == DialogResult.OK)
{
System.IO.FileInfo fiImage = new System.IO.FileInfo(ccd.FileName);
parentForm.facebookService1.UploadPhoto(album.AlbumId, fiImage);
}
}
The UploadPhoto method takes an albumid and a FileInfo that contains the details of the image file. The FileInfo constructor is passed the full file path from the camera dialog.