public IList <ScanInfo> GetScans(bool includeUnknown) { string[] files = null; DirectoryInfo info = new DirectoryInfo(ScanDirectory); if (!includeUnknown) { string[] patterns = SearchPattern.Split(';'); List <string> filesToAdd = new List <string>(); foreach (string pattern in patterns) { var filtered = info.GetFiles(pattern, SearchOption.TopDirectoryOnly).Where(f => (f.Attributes & FileAttributes.Hidden) == 0).Select(f => f.FullName); filesToAdd.AddRange(filtered.ToList()); } files = filesToAdd.ToArray(); } else { files = info.GetFiles("*.*", SearchOption.TopDirectoryOnly).Where(f => (f.Attributes & FileAttributes.Hidden) == 0).Select(f => f.FullName).ToArray(); } IPreviewGenerator previewGen; List <ScanInfo> items = new List <ScanInfo>(); foreach (string scanFile in files) { byte[] scan = File.ReadAllBytes(scanFile); if (scan.Length == 0) { continue; } try { byte[] preview = null; byte[] zoom = null; int pageCount = 1; string fileExtension = Path.GetExtension(scanFile).ToLower(); if (fileExtension.Contains("tif") || fileExtension.Contains("gif") || fileExtension.Contains("jpg") || fileExtension.Contains("jpeg") || fileExtension.Contains("png") || fileExtension.Contains("bmp") || fileExtension.Contains("pdf")) { previewGen = PreviewGeneratorFactory.Create(getMimeType(fileExtension), null, TempDirectory); Image tmpPreview = previewGen.GetPreview(scan); using (MemoryStream ms = new MemoryStream()) { tmpPreview.Save(ms, ImageFormat.Png); preview = ms.ToArray(); } previewGen.PreviewSize = 640; tmpPreview = previewGen.GetPreview(scan); using (MemoryStream ms = new MemoryStream()) { tmpPreview.Save(ms, ImageFormat.Png); zoom = ms.ToArray(); } if (fileExtension.Contains("pdf")) { using (MemoryStream ms = new MemoryStream()) { ms.Write(scan, 0, scan.Length); pageCount = Winnovative.PdfCreator.Document.GetPageCount(ms); } } } items.Add(new ScanInfo() { FileName = Path.GetFileName(scanFile) , FullFileName = scanFile , Scan = scan , ScanPreview = preview , ScanZoom = zoom , MimeType = getMimeType(Path.GetExtension(scanFile)) , PageCount = pageCount }); } catch { continue; } } return(items); }
public ScanInfo GetScanFromFile(byte[] fileContent, string fileName) { ScanInfo scan = null; byte[] preview = null; byte[] zoom = null; int pageCount = 1; string fileExtension = Path.GetExtension(fileName).ToLower(); IPreviewGenerator previewGen = PreviewGeneratorFactory.Create(getMimeType(fileExtension), null, TempDirectory); if (fileExtension.Contains("tif") || fileExtension.Contains("gif") || fileExtension.Contains("jpg") || fileExtension.Contains("jpeg") || fileExtension.Contains("png") || fileExtension.Contains("bmp") || fileExtension.Contains("pdf")) { Image tmpPreview = previewGen.GetPreview(fileContent); using (MemoryStream ms = new MemoryStream()) { tmpPreview.Save(ms, ImageFormat.Png); preview = ms.ToArray(); } previewGen.PreviewSize = 640; tmpPreview = previewGen.GetPreview(fileContent); using (MemoryStream ms = new MemoryStream()) { tmpPreview.Save(ms, ImageFormat.Png); zoom = ms.ToArray(); } if (fileExtension.Contains("pdf")) { using (MemoryStream ms = new MemoryStream()) { ms.Write(fileContent, 0, fileContent.Length); pageCount = Winnovative.PdfCreator.Document.GetPageCount(ms); } } } scan = new ScanInfo() { FileName = Path.GetFileName(fileName) , FullFileName = fileName , Scan = fileContent , ScanPreview = preview , ScanZoom = zoom , MimeType = getMimeType(fileExtension) , PageCount = pageCount }; return(scan); }