public string GetPathToResizedImage(string originalPath)
{
//don't mess with Bloom UI images
if (new[] {"/img/","placeHolder", "Button"}.Any(s => originalPath.Contains(s)))
return originalPath;
string resizedPath;
if(_paths.TryGetValue(originalPath, out resizedPath))
{
if (File.Exists(resizedPath) && new FileInfo(originalPath).LastWriteTimeUtc <= new FileInfo(resizedPath).LastWriteTimeUtc)
{
return resizedPath;
}
else
{
_paths.Remove(originalPath);
}
}
using (var originalImage = PalasoImage.FromFile(originalPath))
{
if (!originalPath.Contains("Button") && !ImageUtils.AppearsToBeJpeg(originalImage))
{
((Bitmap)originalImage.Image).MakeTransparent(Color.White); //instead of white, show the background color
}
if (originalImage.Image.Width > TargetDimension || originalImage.Image.Height > TargetDimension)
{
var maxDimension = Math.Max(originalImage.Image.Width, originalImage.Image.Height);
double shrinkFactor = (TargetDimension/(double) maxDimension);
var destWidth = (int) (shrinkFactor*originalImage.Image.Width);
var destHeight = (int) (shrinkFactor*originalImage.Image.Height);
using (var b = new Bitmap(destWidth, destHeight))
{
using (Graphics g = Graphics.FromImage((Image) b))
{
//in version 1.0, we used .NearestNeighbor. But if there is a border line down the right size (as is common for thumbnails that,
//are, for example, re-inserted into Teacher's Guides), then the line gets cut off. So I switched it to HighQualityBicubic
g.InterpolationMode = InterpolationMode.HighQualityBicubic; //.NearestNeighbor;//or smooth it: HighQualityBicubic
g.DrawImage(originalImage.Image, 0, 0, destWidth, destHeight);
}
var temp = Path.Combine(_cacheFolder, Path.GetRandomFileName() + Path.GetExtension(originalPath));
//Hatton July 2012:
//Once or twice I saw a GDI+ error on the Save below, when the app 1st launched.
//I verified that if there is an IO error, that's what it you get (a GDI+ error).
//I looked once, and the %temp%/Bloom directory wasn't there, so that's what I think caused the error.
//It's not clear why the temp/bloom directory isn't there... possibly it was there a moment ago
//but then some startup thread cleared and deleted it? (we are now running on a thread responding to the http request)
Exception error = null;
for (int i = 0; i < 5; i++) //try up to five times, a second apart
{
try
{
error = null;
if (!Directory.Exists(Path.GetDirectoryName(temp)))
{
Directory.CreateDirectory(Path.GetDirectoryName(temp));
}
b.Save(temp, originalImage.Image.RawFormat);
break;
}
catch (Exception e)
{
Logger.WriteEvent("Error in LowResImage while trying to write image.");
Logger.WriteEvent(e.Message);
error = e;
Thread.Sleep(1000); //wait a second before trying again
}
}
if (error != null)
{
//NB: this will be on a non-UI thread, so it probably won't work well!
ErrorReport.NotifyUserOfProblem(error,
"Bloom is having problem saving a low-res version to your temp directory, at " + temp +
"\r\n\r\nYou might want to quit and restart Bloom. In the meantime, Bloom will try to use the full-res images.");
return originalPath;
}
try
{
_paths.Add(originalPath, temp); //remember it so we can reuse if they show it again, and later delete
}
catch (Exception)
{
// it happens sometimes that though it wasn't in the _paths when we entered, it is now
// I haven't tracked it down... possibly we get a new request for the image while we're busy compressing it?
}
return temp;
}
}
else
{
return originalPath;
}
}
}