");
foreach (IncludedImage includedImage in includedImages)
{
try
{
string useName = includedImage.Name;
string extension = Path.GetExtension(useName).ToLower();
if (!extension.Equals(".jpg") && !extension.Equals(".gif") && !extension.Equals(".png")) // supported formats
{
useName = Path.ChangeExtension(useName, "jpg");
}
string useNameThumbnail = Path.ChangeExtension(includedImage.Name, "jpg");
{
backgroundWorker.ReportProgress((idx * 100) / includedImages.Count, "Building gallery (loading " + includedImage.Name + ")");
if (backgroundWorker.CancellationPending)
{
throw new CancelledException();
}
string fullPath = Path.Combine(Directory, Path.Combine("images", useName));
Bitmap bitmap = null;
// prepare main image
if (!fullPath.Equals(includedImage.LastSavedAs) || !File.Exists(fullPath) || includedImage.LastSaveAsMaxWidth != MaxImageWidth || includedImage.LastSaveWithRotations != includedImage.Rotations)
{
backgroundWorker.ReportProgress((int)((0.2f + idx) * 100) / includedImages.Count, "Building gallery (sizing " + includedImage.Name + ")");
if (backgroundWorker.CancellationPending)
{
throw new CancelledException();
}
Image output;
Image resized = null;
bitmap = includedImage.GetBitmap();
if (bitmap.Width > MaxImageWidth)
{
resized = ResizeBitmap(bitmap, MaxImageWidth);
output = resized;
}
else
{
output = bitmap;
}
backgroundWorker.ReportProgress((int)((0.4f + idx) * 100) / includedImages.Count, "Building gallery (saving " + includedImage.Name + ")");
if (backgroundWorker.CancellationPending)
{
throw new CancelledException();
}
ImageServices.SaveImage(output, fullPath, Program.Settings.Gallery.JpegQuality);
if (resized != null)
{
resized.Dispose();
}
includedImage.LastSavedAs = fullPath;
includedImage.LastSaveAsMaxWidth = MaxImageWidth;
includedImage.LastSaveWithRotations = includedImage.Rotations;
}
string fullPathThumbnail = Path.Combine(Directory, Path.Combine("thumbs", useNameThumbnail));
// prepare thumbnail image
if (!fullPathThumbnail.Equals(includedImage.LastSavedAsThumbnail) || !File.Exists(fullPathThumbnail) || includedImage.LastSavedAsThumbnailSize != ThumbnailSize || includedImage.LastSaveWithRotationsThumbnail != includedImage.Rotations)
{
backgroundWorker.ReportProgress((int)((0.6f + idx) * 100) / includedImages.Count, "Building gallery (sizing thumbnail " + useName + ")");
if (backgroundWorker.CancellationPending)
{
throw new CancelledException();
}
if (bitmap == null)
{
bitmap = includedImage.GetBitmap();
}
float scale = (float)Math.Sqrt((double)(ThumbnailSize * ThumbnailSize) / (bitmap.Width * bitmap.Height));
using (Bitmap output = ScaleBitmap(bitmap, scale))
{
backgroundWorker.ReportProgress((int)((0.8f + idx) * 100) / includedImages.Count, "Building gallery (saving thumbnail " + useName + ")");
if (backgroundWorker.CancellationPending)
{
throw new CancelledException();
}
ImageServices.SaveImage(output, fullPathThumbnail, Program.Settings.Gallery.JpegQuality);
}
}
includedImage.LastSavedAsThumbnailSize = ThumbnailSize;
includedImage.LastSavedAsThumbnail = fullPathThumbnail;
includedImage.LastSaveWithRotationsThumbnail = includedImage.Rotations;
if (bitmap != null)
{
bitmap.Dispose();
}
}
if (!openRow)
{
tw.WriteLine(@"");
openRow = true;
}
tw.WriteLine(" | ");
thumbnailsSoFar++;
if (thumbnailsSoFar == ThumbnailsPerLine)
{
tw.WriteLine(@"
");
openRow = false;
thumbnailsSoFar = 0;
}
}
catch (CancelledException exception)
{
throw exception;
}
catch (Exception e)
{
errors.AppendLine(@"" + includedImage.Name + @":
");
errors.AppendLine(@"" + e.ToString() + @":
");
}
idx++;
}
if (openRow)
{
tw.WriteLine(@"");
}
tw.WriteLine(@"
");
}
else
{
tw.WriteLine(line);
}
}
}
}
}
catch (CancelledException cancelledException)
{
throw cancelledException;
}
catch (InvalidOperationException)
{
// this is collection collision .. benign so just cancel
throw new CancelledException();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error building gallery");
throw new CancelledException();
}
//tw.Close();
}
///