/// <summary> /// Select PDF page and returns page images as single image. /// </summary> public static BitmapSource SelectPdfPageImage(string filename, ref int pageIndex) { // create PdfImageViewer PdfImageViewer viewer; try { viewer = new PdfImageViewer(filename); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } try { // collect pages with images List <int> pageWithImages = new List <int>(); for (int i = 0; i < viewer.PageCount; i++) { string[] imageNames = viewer.GetImageNames(i); if (imageNames.Length > 0) { pageWithImages.Add(i); } } if (pageWithImages.Count == 0) { MessageBox.Show("Images in PDF file are not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } if (pageWithImages.Count > 1) { // select page SelectPdfPageWindow selectPdfPageWindow = new SelectPdfPageWindow(pageWithImages.ToArray()); selectPdfPageWindow.ShowDialog(); pageIndex = pageWithImages[selectPdfPageWindow.pagesComboBox.SelectedIndex]; } else { pageIndex = pageWithImages[0]; } return(GetPdfPageImage(viewer, pageIndex)); } finally { viewer.Dispose(); } }
/// <summary> /// Loads image from specified file. /// </summary> private BitmapSource LoadImage(string filename, ref int pageIndex) { if (_imageFileStream != null) { _imageFileStream.Dispose(); _imageFileStream = null; } string fileExt = System.IO.Path.GetExtension(filename).ToUpper(); switch (fileExt) { // PDF file case ".PDF": if (BarcodeGlobalSettings.IsDemoVersion) { MessageBox.Show("Evaluation version allows to extract images only from the first page of PDF document.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } if (pageIndex == -1) { return(SelectPdfPageWindow.SelectPdfPageImage(filename, ref pageIndex)); } else { return(SelectPdfPageWindow.GetPdfPageImage(filename, pageIndex)); } // TIFF file case ".TIF": case ".TIFF": _imageFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read); TiffBitmapDecoder tiffDecoder = new TiffBitmapDecoder(_imageFileStream, BitmapCreateOptions.None, BitmapCacheOption.Default); if (pageIndex == -1 && tiffDecoder.Frames.Count > 1) { pageIndex = SelectImageFrameWindow.SelectFrameIndex(tiffDecoder.Frames.Count); return(tiffDecoder.Frames[pageIndex]); } return(tiffDecoder.Frames[0]); // image default: _imageFileStream = new FileStream(filename, FileMode.Open, FileAccess.Read); BitmapImage image = new BitmapImage(); image.BeginInit(); image.StreamSource = _imageFileStream; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); return(image); } }