/// <summary> /// Treats the given image and saves the result. /// Creates the chapter file needed for the epub related to the image. /// If the image width / height ratio is over 0.75, it will not be treated. /// </summary> /// <param name="imageOriginal">Image to be treated.</param> /// <param name="imageIndex">Index of the current image in the list of treated images.</param> /// <param name="imagePath">Path of the image to treat.</param> private void SaveImage(Bitmap imageOriginal, ref int imageIndex, string imagePath) { if (imageOriginal.Width / imageOriginal.Height > 0.75) { ratioErrors.Add(imagePath); return; } using (Bitmap treatedImage = ImageTreater.GetInstance().TreatImage(imageOriginal, Data.Height, Data.Grayscale, Data.Trimming, Data.TrimmingValue, Data.LeftMargin, Data.TrimmingMethod)) { treatedImage.Save(imagesFolderPath + "I" + imageIndex.ToString() + ".jpg", codec, parameters); } builderContent1.AppendLine("\t\t<item xmlns=\"\" id=\"P" + imageIndex.ToString() + "\" href=\"P" + imageIndex.ToString() + ".xml\" media-type=\"application/xhtml+xml\"/>"); builderContent2.AppendLine("\t\t<itemref xmlns=\"\" idref=\"P" + imageIndex.ToString() + "\" linear=\"yes\"/>"); builderToc1.AppendLine("\t\t<navPoint xmlns=\"\" id=\"N" + imageIndex.ToString() + "\" playOrder=\"" + imageIndex.ToString() + "\"><navLabel><text>P" + imageIndex.ToString() + "</text></navLabel><content src=\"P" + imageIndex.ToString() + ".xml\"/></navPoint>"); StringBuilder chapter = new StringBuilder(); chapter.Append(builderChapter1.ToString()); chapter.AppendLine("\t\t<img style=\"margin:0\" src=\"Images/I" + imageIndex.ToString() + ".jpg\" />"); chapter.Append(builderChapter2.ToString()); StreamWriter chapterWriter = new StreamWriter(oebpsFolderPath + "P" + imageIndex.ToString() + ".xml", false); chapterWriter.Write(chapter.ToString()); chapterWriter.Close(); imageIndex++; }
/// <summary> /// Attempts to read the image files and checks if they are Double Pages. /// Sends each of the resulting images for treatment. /// </summary> private void ProcessImages() { int imageIndex = 1; foreach (string path in Data.Files) { Bitmap from = null; try { from = new Bitmap(path); } catch { imageErrors.Add(path); } if (from == null) { continue; } using (from) { List <Bitmap> images = ImageTreater.GetInstance().HandleDoublePage(from, Data.DoublePage, Data.Offset); foreach (Bitmap image in images) { SaveImage(image, ref imageIndex, path); image.Dispose(); } } TreatedImages++; } }
private void SetImage2(Bitmap bitmap, MemoryStream stream) { using (Bitmap treatedImage = ImageTreater.GetInstance().TreatImage(bitmap, Data.Height, Data.Grayscale, Data.Trimming, Data.TrimmingValue, Data.LeftMargin, Data.TrimmingMethod)) { treatedImage.Save(stream, codec, parameters); } Image2 = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); }
/// <summary> /// Returns the single instance of the ImageTreater class. /// </summary> /// <returns>A single instance of the ImageTreater class.</returns> public static ImageTreater GetInstance() { lock (typeof(ImageTreater)) { if (instance == null) { instance = new ImageTreater(); } } return(instance); }
/// <summary> /// Displays the selected image as it will appear in the epub file. /// </summary> private void TreatImage() { ImagePath = Data.Files[currentImageIndex]; using (MemoryStream stream = new MemoryStream()) { Bitmap imageToTreat = null; try { imageToTreat = new Bitmap(Data.Files[currentImageIndex]); } catch { Image1 = null; Image2 = null; Error = Data.Files[currentImageIndex]; ShowError = true; } if (imageToTreat != null) { ShowError = false; using (imageToTreat) { List <Bitmap> images = ImageTreater.GetInstance().HandleDoublePage(imageToTreat, Data.DoublePage, Data.Offset); if (images.Count == 1) { SetImage1(images[0], stream); images[0].Dispose(); Image2 = null; } else if (images.Count == 2) { SetImage1(images[0], stream); images[0].Dispose(); stream.Flush(); SetImage2(images[1], stream); images[1].Dispose(); } } } } }