public string OutputFile(string path, string page, string outputPath) { int pageInt = int.Parse(page); int numberOfPages = GetNumberOfPages(path); if (numberOfPages == 0) { throw new MangaWrongFormatException(path); } if (pageInt < 1 || pageInt > numberOfPages) { throw new MangaContentMismatchException(path); } outputPath = outputPath + ".png"; using (PdfFile pdfFile = new PdfFile(path)) { lock (pdfFile.Wrapper) { pdfFile.Wrapper.CurrentPage = pageInt; using (PictureBox pictureBox = new PictureBox()) { pictureBox.Width = pictureBox.Height = 3000; pdfFile.Wrapper.FitToHeight(pictureBox.Handle); pictureBox.Width = pdfFile.Wrapper.PageWidth; pictureBox.Height = pdfFile.Wrapper.PageHeight; pdfFile.Wrapper.RenderPage(pictureBox.Handle); using (Bitmap image = new Bitmap(pdfFile.Wrapper.PageWidth, pdfFile.Wrapper.PageHeight)) { using (Graphics g = Graphics.FromImage(image)) { pdfFile.Wrapper.ClientBounds = new Rectangle(0, 0, pdfFile.Wrapper.PageWidth, pdfFile.Wrapper.PageHeight); pdfFile.Wrapper.CurrentX = pdfFile.Wrapper.CurrentY = 0; IntPtr ptr = g.GetHdc(); pdfFile.Wrapper.DrawPageHDC(ptr); g.ReleaseHdc(ptr); } using (FileStream outputFile = new FileStream(outputPath, FileMode.Create)) { image.Save(outputFile, ImageFormat.Png); } } } } } return outputPath; }
private int GetNumberOfPages(string path) { try { using (PdfFile pdfFile = new PdfFile(path)) { lock (pdfFile.Wrapper) { return pdfFile.Wrapper.PageCount; } } } catch (Exception) { return 0; } }