BitmapImage CreatePage(int p, string t = "Full") { int i; if (t == "Mini") { i = 100; } else { i = Convert.ToInt32(FilePickerT.ActualWidth); } using (var memoryStream = new MemoryStream(Pages[p])) using (WrappingStream wrapper = new WrappingStream(memoryStream)) { BitmapImage Image = new BitmapImage(); Image.BeginInit(); Image.StreamSource = wrapper; Image.CacheOption = BitmapCacheOption.OnLoad; Image.DecodePixelWidth = i; Image.EndInit(); Image.Freeze(); return(Image); } }
public void CreateThumbnail(string FilePath, int p = 1) { Dictionary <int, byte[]> Cover = new Dictionary <int, byte[]>(); Cover.Clear(); string type = Path.GetExtension(FilePath).ToLower(); // use the apropriate function for compressed or pdf file if (System.IO.Path.GetExtension(FilePath) == ".pdf") { //Call the function to load the pdf (not the pages) //the function that load the page is called within the reader.cs //since on a pdf the page is loaded on demand for memory efficiency purpose // Program.LoadPDF(FilePath); // currentBook.TotalPages = Program.PDFBook.TotalPage; return; } else { ArchiveLoader(FilePath); } BitmapImage a = CreateCover(1); BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(a)); string cover = Path.GetDirectoryName(FilePath) + "/.metadata/" + Path.GetFileNameWithoutExtension(FilePath) + ".jpg"; using (var fileStream = new System.IO.FileStream(cover, System.IO.FileMode.Create)) { encoder.Save(fileStream); } GC.Collect(); void ArchiveLoader(string Path) { int i = 0; var archive = ArchiveFactory.Open(Path); foreach (var entry in archive.Entries) { //Check if the entries in the File are : not a directoy AND contain in their name .jpg OR .png if (!entry.IsDirectory & (entry.Key.ToLower().Contains(".jpg") | entry.Key.ToLower().Contains(".png"))) { i++; if (i <= 1) { using (MemoryStream MemStream = new MemoryStream()) { entry.WriteTo(MemStream); MemStream.Seek(0, SeekOrigin.Begin); byte[] bytes = MemStream.ToArray(); Cover.Add(i, bytes); bytes = null; } } else { break; } } } archive = null; } BitmapImage CreateCover(int c) { using (var memoryStream = new MemoryStream(Cover[c])) using (WrappingStream wrapper = new WrappingStream(memoryStream)) { BitmapImage Image = new BitmapImage(); Image.BeginInit(); Image.StreamSource = wrapper; Image.CacheOption = BitmapCacheOption.OnLoad; // Image.DecodePixelWidth = Convert.ToInt32(FilePickerT.ActualWidth); Image.DecodePixelWidth = 200; Image.EndInit(); Image.Freeze(); return(Image); } } }