I provide a sample ThumbnailManager, working with an IsolatedStorageFile.
public class ThumbnailManager : IThumbnailManagerI deal with host names because I am also working on an implementation dealing with many shares on the network. The GetThumbnail method just resizes pictures.
{
#region Fields
private readonly IsolatedStorageFile store;
#endregion
public ThumbnailManager()
{
store = IsolatedStorageFile.GetUserStoreForAssembly();
}
public ImageSource GetThumbnail(string host, string path)
{
string thumbName = Path.GetFileName(path);
if (store.GetFileNames(thumbName).Length == 0)
{
using (var stream = new IsolatedStorageFileStream(thumbName, FileMode.CreateNew, store))
{
byte[] data = GetThumbnail(path);
stream.Write(data, 0, data.Length);
}
}
using (var stream = new IsolatedStorageFileStream(thumbName, FileMode.Open, store))
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
image.Freeze();
return image;
}
}
}
private byte[] GetThumbnail(string path)For the sample videos on youtube, I downloaded many covers from amazon. That's why I need the helper function that removes the blank frame around the picture:
{
Image source = Image.FromFile(path);
source = AmazonCut(source);
int height = source.Height;
int width = source.Width;
int factor = (height - 1) / 250 + 1;
int smallHeight = height / factor;
int smallWidth = width / factor;
Image thumb = source.GetThumbnailImage(smallWidth, smallHeight, null, IntPtr.Zero);
using (var ms = new MemoryStream())
{
thumb.Save(ms, ImageFormat.Png);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
var result = new byte[ms.Length];
ms.Read(result, 0, (int)ms.Length);
return result;
}
}
private static Image AmazonCut(Image image)Well this whole class is not perfect code, but it is sufficient for a demo.
{
if (image.Width != image.Height)
return image;
var bmp = new Bitmap(image);
int size = image.Height;
int white = System.Drawing.Color.FromKnownColor(KnownColor.White).ToArgb();
int i = 0;
while (i < size / 2)
{
if (bmp.GetPixel(i, i).ToArgb() != white)
break;
if (bmp.GetPixel(i, size - 1 - i).ToArgb() != white)
break;
if (bmp.GetPixel(size - 1 - i, i).ToArgb() != white)
break;
if (bmp.GetPixel(size - 1 - i, size - 1 - i).ToArgb() != white)
break;
i++;
}
if (i > 0)
{
i += 8;
var zone = new Rectangle(i, i, size - 2 * i, size - 2 * i);
return bmp.Clone(zone, System.Drawing.Imaging.PixelFormat.DontCare);
}
return bmp;
}
If you do not know innerings of IsolatedStorage, you will find the thumbnails in a folder like C:\Documents and Settings\ded\Local Settings\Application Data\IsolatedStorage\0ypqvhod.rll\j4pydd3v.g4v\StrongName.alr3sbzvfezz2fk22sd5g0b5dxbwzr0b\AssemFiles.
Edit 2014-02-23 : Code has moved to github.