public void SaveDocumentPagesToImages(IDocumentPaginatorSource document, string filename) { if (string.IsNullOrEmpty(filename)) { return; } MemoryStream[] streams = null; try { int pageCount = document.DocumentPaginator.PageCount; DocumentPage[] pages = new DocumentPage[pageCount]; for (int i = 0; i < pageCount; i++) { pages[i] = document.DocumentPaginator.GetPage(i); } streams = new MemoryStream[pages.Count()]; for (int i = 0; i < pages.Count(); i++) { DocumentPage source = pages[i]; streams[i] = new MemoryStream(); RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)source.Size.Width, (int)1500, 96, // WPF (Avalon) units are 96dpi based 96, System.Windows.Media.PixelFormats.Default); renderTarget.Render(source.Visual); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc encoder.QualityLevel = 100; encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(streams[i]); FileStream file = new FileStream(filename, FileMode.CreateNew); file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length); file.Close(); streams[i].Position = 0; } } catch (Exception e1) { throw e1; } finally { if (streams != null) { foreach (MemoryStream stream in streams) { stream.Close(); stream.Dispose(); } } } }
public void SaveDocumentPagesToImages(IDocumentPaginatorSource document, string dirPath) { if (string.IsNullOrEmpty(dirPath)) { return; } if (dirPath[dirPath.Length - 1] != '\\') { dirPath += "\\"; } if (!Directory.Exists(dirPath)) { return; } MemoryStream[] streams = null; try { int pageCount = document.DocumentPaginator.PageCount; DocumentPage[] pages = new DocumentPage[pageCount]; for (int i = 0; i < pageCount; i++) { pages[i] = document.DocumentPaginator.GetPage(i); } streams = new MemoryStream[pages.Count()]; for (int i = 0; i < pages.Count(); i++) { DocumentPage source = pages[i]; streams[i] = new MemoryStream(); RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)source.Size.Width, (int)source.Size.Height, 96, // WPF (Avalon) units are 96dpi based 96, PixelFormats.Default); renderTarget.Render(source.Visual); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc encoder.QualityLevel = 100; encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(streams[i]); FileStream file = new FileStream(dirPath + Path.GetFileNameWithoutExtension(selectedfile) + "_" + (i + 1) + ".jpg", FileMode.CreateNew); file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length); file.Close(); mainimagetext = Path.Combine(sDirPath, Path.GetFileNameWithoutExtension(selectedfile) + "_" + (i + 1) + ".jpg"); streams[i].Position = 0; } } catch (Exception e1) { throw e1; } finally { if (streams != null) { foreach (MemoryStream stream in streams) { stream.Close(); stream.Dispose(); } } } }
//public void openthisinviewer(int index) //{ // XpsDocument xpsdoc = new XpsDocument(_pageurls[index], System.IO.FileAccess.Read); // this.documentViewer.Document = xpsdoc.GetFixedDocumentSequence(); //} public string SaveDocumentPagesToImages(string dirPath) { XpsDocument doc = new XpsDocument(dirPath, FileAccess.Read); FixedDocumentSequence document = doc.GetFixedDocumentSequence(); string newpath = ""; MemoryStream[] streams = null; try { int pageCount = document.DocumentPaginator.PageCount ; DocumentPage[] pages = new DocumentPage[pageCount]; for (int i = 0; i < pageCount; i++) pages[i] = document.DocumentPaginator.GetPage(i); streams = new MemoryStream[pages.Count()]; for (int i = 0; i < pages.Count(); i++) { DocumentPage source = pages[i]; streams[i] = new MemoryStream(); RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)source.Size.Width, (int)source.Size.Height, 96, // WPF (Avalon) units are 96dpi based 96, System.Windows.Media.PixelFormats.Default); renderTarget.Render(source.Visual); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc encoder.Rotation = Rotation.Rotate270; encoder.QualityLevel = 100; encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(streams[i]); newpath = dirPath.Replace(".xps", ".jpg"); FileStream file = new FileStream(newpath, FileMode.CreateNew); file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length); file.Close(); streams[i].Position = 0; } } catch (Exception e1) { throw e1; } finally { if (streams != null) { foreach (MemoryStream stream in streams) { stream.Close(); stream.Dispose(); doc.Close(); } } } return newpath; }