33. Thumbnail web page

This solution is similar to the preceding solution. The main differences lie in the ProcessFiles method. The following code shows this method with the differences highlighted:

// Process the files.
private void ProcessFiles()
{
// Get the input parameters.
string dirname = directoryTextBox.Text;
int thumbWidth = int.Parse(widthTextBox.Text);
int thumbHeight = int.Parse(heightTextBox.Text);

// Graphic file name patterns.
string[] patterns = { "*.png", "*.bmp", "*.jpg", "*.jpeg",
"*.gif" };

// Make a list of the directory's image files.
List<string> filenames = new List<string>();
foreach (string pattern in patterns)
filenames.AddRange(Directory.GetFiles(dirname, pattern));

// Compose the thumbnail directory's name.
string thumbdir = Path.Combine(dirname, "Thumbnails");

// Create an empty thumbnail directory.
EmptyDirectory(thumbdir);
Directory.CreateDirectory(thumbdir);

// Start the web page.
StringBuilder sb = new StringBuilder();
sb.AppendLine("<html>");
sb.AppendLine("<body>");

// Process the files.
foreach (string filename in filenames)
{
// Create the thumbnail.
Bitmap bm = MakeThumbnail(filename, thumbWidth, thumbHeight);
string thumbname = Path.Combine(thumbdir,
Path.GetFileNameWithoutExtension(filename)) +
" thumb.bmp";
bm.Save(thumbname);

// Add an entry to the web page.
FileInfo fileinfo = new FileInfo(filename);
string bigFilename = Path.Combine("..", fileinfo.Name);
FileInfo thumbinfo = new FileInfo(thumbname);
string thumbFilename = thumbinfo.Name;
sb.AppendLine(" <a href="" + bigFilename + "">" +
"<img src="" + thumbFilename + "">" +
"</a>");
}
sb.AppendLine("</body>");
sb.AppendLine("</html>");

// Write the web page.
string webFilename = Path.Combine(thumbdir, "Thumbnails.html");
File.WriteAllText(webFilename, sb.ToString());

// Display the web page in the system's default browser.
System.Diagnostics.Process.Start(webFilename);

numCreatedLabel.Text = $"Created {filenames.Count} thumbnails";
}

The method begins as the previous version did. It then creates a StringBuilder to hold the web page and adds <html> and <body> elements to it.

The code then creates the thumbnails as before. For each thumbnail, it also adds a new line to the web page with the following format:

<a href="..anner.png"><img src="banner thumb.bmp"></a>

This is a link to the banner.png file in the current directory's parent directory. The link displays the picture in the banner thumb.bmp file inside the current directory.

After it processes all of the files, the method adds the closing </body> and </html> elements and writes the StringBuilder contents into the web page file.

The final new piece of the method is the following statement:

System.Diagnostics.Process.Start(webFilename);

This statement makes the system open the file named webFilename. The system opens the file with the default program associated with that type of file. This file's name ends with the .html extension, so the system opens it with its default web browser.

You might prefer to display the new web page within the program in a WebBrowser control. Unfortunately, that control seems to be unable to work with relative image paths such as banner thumb.bmp, so it cannot correctly display this web page. You could modify the code to use absolute paths to the image files, but then the web page would be less useful on an actual website.

Download the ThumbnailWebPage example solution to see additional details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.100.89