Exemplo n.º 1
0
        /// <summary>
        /// extract an image from a zipped content for the given filename
        /// </summary>
        /// <param name="zipFilePath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public BitmapImage GetImageFromStream(string zipFilePath, string fileName)
        {
            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("BookService.GetImageFromStream");
            }

            SevenZipExtractor temp   = null;
            BitmapImage       result = null;

            try
            {
                temp = ZipHelper.Instance.GetExtractor(zipFilePath);

                MemoryStream stream = new MemoryStream();
                temp.ExtractFile(fileName, stream);

                result = StreamToImage.GetImageFromStreamBug(stream, 0);
            }
            catch (Exception err)
            {
                LogHelper.Manage("BookService:GetImageFromStream", err);
            }
            finally
            {
                ZipHelper.Instance.ReleaseExtractor(temp);
                LogHelper.End("BookService.GetImageFromStream");
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generate a catalog cover from several books
        /// </summary>
        /// <param name="catlog"></param>
        public BitmapImage GenerateCatalogCover(Catalog catlog, bool useFile)
        {
            try
            {
                BitmapImage bmpResult;
                string      file = Path.Combine(DirectoryHelper.CachePath, Path.GetFileNameWithoutExtension(catlog.CatalogFilePath) + ".png");

                if (File.Exists(file))
                {
                    catlog.CoverUri = new Uri(file);
                    bmpResult       = new BitmapImage(catlog.CoverUri);
                }
                else
                {
                    BookInfoService    srv  = new BookInfoService();
                    List <BitmapImage> bmps = new List <BitmapImage>();
                    for (int i = 0; i <= 3 && i < catlog.BookInfoFilePath.Count; i++)
                    {
                        bmps.Add(srv.ExtractBookCover(catlog.BookInfoFilePath[i]));
                    }

                    RenderTargetBitmap temp = new RenderTargetBitmap((int)150, (int)150, 96d, 96d, PixelFormats.Pbgra32);

                    DrawingVisual dv = new DrawingVisual();
                    using (DrawingContext ctx = dv.RenderOpen())
                    {
                        for (int i = 0; i < bmps.Count; i++)
                        {
                            ctx.DrawImage(bmps[i], new System.Windows.Rect(i * 30, i * 30, bmps[i].PixelWidth, bmps[i].PixelHeight));
                        }
                        ctx.Close();
                    }

                    temp.Render(dv);
                    bmps.Clear();

                    using (MemoryStream memStream = new MemoryStream())
                    {
                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(temp));
                        encoder.Save(memStream);
                        bmpResult = StreamToImage.GetImageFromStreamBug(memStream, 0);
                    }

                    using (FileStream filStream = new FileStream(file, FileMode.Create))
                    {
                        PngBitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(temp));
                        encoder.Save(filStream);
                        catlog.CoverUri = new Uri(file);
                    }
                }
                return(bmpResult);
            }
            catch (Exception err)
            {
                LogHelper.Manage("CatalogService.GenerateCatalogCover", err);
                return(null);
            }
        }