WPF Cover Flow Tutorial : Part 7 (source)

来源:互联网 发布:淘宝网唢呐哨盒 编辑:程序博客网 时间:2024/05/01 07:33
Before disclosing sources of Part 7, here a few notes.

I provide a sample ThumbnailManager, working with an IsolatedStorageFile.
view plainprint?
  1. public class ThumbnailManager : IThumbnailManager  
  2. {  
  3.     #region Fields  
  4.     private readonly IsolatedStorageFile store;  
  5.     #endregion  
  6.     public ThumbnailManager()  
  7.     {  
  8.         store = IsolatedStorageFile.GetUserStoreForAssembly();  
  9.     }  
  10.     public ImageSource GetThumbnail(string host, string path)  
  11.     {  
  12.         string thumbName = Path.GetFileName(path);  
  13.         if (store.GetFileNames(thumbName).Length == 0)  
  14.         {  
  15.             using (var stream = new IsolatedStorageFileStream(thumbName, FileMode.CreateNew, store))  
  16.             {  
  17.                 byte[] data = GetThumbnail(path);  
  18.                 stream.Write(data, 0, data.Length);  
  19.             }  
  20.         }  
  21.         using (var stream = new IsolatedStorageFileStream(thumbName, FileMode.Open, store))  
  22.         {  
  23.             var image = new BitmapImage();  
  24.             image.BeginInit();  
  25.             image.CacheOption = BitmapCacheOption.OnLoad;  
  26.             image.StreamSource = stream;  
  27.             image.EndInit();  
  28.             image.Freeze();  
  29.             return image;  
  30.         }  
  31.     }  
  32. }  
I 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.
view plainprint?
  1. private byte[] GetThumbnail(string path)  
  2. {  
  3.     Image source = Image.FromFile(path);  
  4.     source = AmazonCut(source);  
  5.     int height = source.Height;  
  6.     int width = source.Width;  
  7.     int factor = (height - 1) / 250 + 1;  
  8.     int smallHeight = height / factor;  
  9.     int smallWidth = width / factor;  
  10.     Image thumb = source.GetThumbnailImage(smallWidth, smallHeight, null, IntPtr.Zero);  
  11.     using (var ms = new MemoryStream())  
  12.     {  
  13.         thumb.Save(ms, ImageFormat.Png);  
  14.         ms.Flush();  
  15.         ms.Seek(0, SeekOrigin.Begin);  
  16.         var result = new byte[ms.Length];  
  17.         ms.Read(result, 0, (int)ms.Length);  
  18.         return result;  
  19.     }  
  20. }  
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:
view plainprint?
  1. private static Image AmazonCut(Image image)  
  2. {  
  3.     if (image.Width != image.Height)  
  4.         return image;  
  5.     var bmp = new Bitmap(image);  
  6.     int size = image.Height;  
  7.     int white = System.Drawing.Color.FromKnownColor(KnownColor.White).ToArgb();  
  8.     int i = 0;  
  9.     while (i < size / 2)  
  10.     {  
  11.         if (bmp.GetPixel(i, i).ToArgb() != white)  
  12.             break;  
  13.         if (bmp.GetPixel(i, size - 1 - i).ToArgb() != white)  
  14.             break;  
  15.         if (bmp.GetPixel(size - 1 - i, i).ToArgb() != white)  
  16.             break;  
  17.         if (bmp.GetPixel(size - 1 - i, size - 1 - i).ToArgb() != white)  
  18.             break;  
  19.         i++;  
  20.     }  
  21.     if (i > 0)  
  22.     {  
  23.         i += 8;  
  24.         var zone = new Rectangle(i, i, size - 2 * i, size - 2 * i);  
  25.         return bmp.Clone(zone, System.Drawing.Imaging.PixelFormat.DontCare);  
  26.     }  
  27.     return bmp;  
  28. }  
Well this whole class is not perfect code, but it is sufficient for a demo.

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.

Here is the source.