Beispiel #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);
        }
Beispiel #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);
            }
        }
Beispiel #3
0
        /// <summary>
        /// string to image
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private BitmapImage ConvertToBitmapImage(string data)
        {
            string stream = data.Split(',')[1];

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(stream)))
            {
                return(StreamToImage.GetImageFromStream(ms, 0));
            }
        }
Beispiel #4
0
        /// <summary>
        /// will merge a group of images into one
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <param name="imageNames"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="index"></param>
        public void MergeGroup(List <byte[]> imageBytes, List <string> imageNames, int start, int end, int index)
        {
            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("ImageJoiner.MergeGroup");
            }
            try
            {
                List <BitmapImage> bmps = new List <BitmapImage>();
                double             maxWidth = 0, maxHeight = 0, position = 0;

                for (int i = start; i <= end; i++)
                {
                    MemoryStream ms      = new MemoryStream(imageBytes[i]);
                    BitmapImage  myImage = new BitmapImage();
                    myImage.BeginInit();
                    myImage.StreamSource = ms;
                    myImage.CacheOption  = BitmapCacheOption.None;
                    myImage.EndInit();

                    bmps.Add(myImage);
                    maxWidth   = Math.Max(myImage.Width, maxWidth);
                    maxHeight += myImage.Height;
                }

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

                DrawingVisual dv = new DrawingVisual();
                using (DrawingContext ctx = dv.RenderOpen())
                {
                    foreach (BitmapImage bi in bmps)
                    {
                        ctx.DrawImage(bi, new System.Windows.Rect(0, position, bi.Width, bi.Height));
                        position += bi.Height;
                    }
                    ctx.Close();
                }

                temp.Render(dv);

                NewImageNames.Add(string.Format("{0:0000}.jpg", index));
                NewImageBytes.Add(StreamToImage.BufferFromImage(temp));

                bmps.Clear();
            }
            catch (Exception err)
            {
                LogHelper.Manage("ImageJoiner.MergeGroup", err);
            }
            finally
            {
                LogHelper.End("ImageJoiner.MergeGroup");
            }
        }
Beispiel #5
0
 public List <WCFCatalog> GetCatalogList()
 {
     try
     {
         return(CatalogService.Instance.CatalogRepository
                .Where(p => p.IsShared)
                .Select(p => new WCFCatalog()
         {
             ID = p.CatalogFilePath,
             BookCount = p.BookInfoFilePath.Count,
             Title = p.Title,
             Description = p.Description,
             Image = StreamToImage.GetByteFromImageFile(p.CoverUri.LocalPath)
         }).ToList());
     }
     catch (Exception)
     {
         throw;
     }
 }
Beispiel #6
0
        public bool Read(string inputFileorFolder, string outputFolder, List <byte[]> imageBytes, List <string> imageNames, ContractParameters settings, ProgressDelegate progress)
        {
            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("ImageFileReader.Read");
            }
            try
            {
                string[] files = Directory.GetFiles(inputFileorFolder, "*.*", SearchOption.TopDirectoryOnly);

                foreach (string filename in files)
                {
                    if (DocumentFactory.Instance.ImageExtension.Contains(Path.GetExtension(filename).ToUpper()))
                    {
                        BitmapImage myImage = new BitmapImage();
                        myImage.BeginInit();
                        myImage.UriSource   = new Uri(filename, UriKind.RelativeOrAbsolute);
                        myImage.CacheOption = BitmapCacheOption.OnLoad;
                        myImage.EndInit();

                        imageBytes.Add(StreamToImage.BufferFromImage(myImage));
                        imageNames.Add(Path.GetFileName(filename));
                    }
                }
                string msg = CultureManager.Instance.GetLocalization("ByCode", "Convert.ImageFound", "{0} images founded...");
                progress(string.Format(msg, imageBytes.Count));
            }
            catch (Exception err)
            {
                LogHelper.Manage("ImageFileReader:Read", err);
                settings.Result = false;
                return(false);
            }
            finally
            {
                LogHelper.End("ImageFileReader.Read");
            }
            return(true);
        }
Beispiel #7
0
        /// <summary>
        /// Save the given BookInfo
        /// </summary>
        /// <param name="param"></param>
        public void SaveBookInfo(object param)
        {
            Book   bk     = param as Book;
            Stream stream = null;

            if (LogHelper.CanDebug())
            {
                LogHelper.Begin("BookInfoService.SaveBookInfo");
            }
            try
            {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(bk.BookInfoFilePath, FileMode.Create, FileAccess.Write, FileShare.None);

                if (stream != null)
                {
                    //the thumbnail
                    using (Stream img = StreamToImage.GetStreamFromImage(bk.Cover))
                    {
                        formatter.Serialize(stream, img);
                    }
                    //mySelf file to be restored
                    formatter.Serialize(stream, bk.BookInfoFilePath);
                    // comic file
                    formatter.Serialize(stream, bk.FilePath);
                    //bookmark tag
                    formatter.Serialize(stream, bk.Bookmark);
                    //IsRead tag
                    formatter.Serialize(stream, bk.IsRead);
                    //IsSecured tag
                    formatter.Serialize(stream, bk.IsSecured);
                    //Password tag
                    formatter.Serialize(stream, bk.Password);
                    //page number
                    formatter.Serialize(stream, bk.PageCount);
                    //Size tag
                    formatter.Serialize(stream, bk.Size);
                    //Rating tag
                    formatter.Serialize(stream, bk.Rating);

                    //manage the dynamic properties
                    IDictionary <string, object> dict = bk.Dynamics as IDictionary <string, object>;
                    int counter = dict.Count(p => !string.IsNullOrEmpty(p.Value.ToString()));

                    //not null property counter
                    formatter.Serialize(stream, counter);

                    //then the key/values
                    foreach (string k in dict.Keys)
                    {
                        if (!string.IsNullOrEmpty(k))
                        {
                            formatter.Serialize(stream, k);
                            formatter.Serialize(stream, dict[k]);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.Manage("BookInfoService.SaveBookInfo", err);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }

                LogHelper.End("BookInfoService.SaveBookInfo");
            }
        }