public ActionResult GalleryGetDocumentThumbnail(string doc) { const int THUMBNAIL_WIDTH = 120; const int THUMBNAIL_HEIGHT = 100; Color thumbnailBackgroundColor = Color.Transparent; if (doc != null) { string docPath = MvcApplication.GetDocumentsDirectory() + "\\" + HttpUtility.UrlDecode(doc); string thumbPath = string.Empty; //thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "DOCX.thumb"; GdPicture14.DocumentFormat documentFormat = new GdPicture14.DocumentFormat(); switch (Path.GetExtension(doc).ToUpper()) { case ".PDF": thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "PDF.thumb"; break; case ".JPEG": thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "JPEG.thumb"; break; case ".DOCX": thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "DOCX.thumb"; break; case ".XLSX": thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "XLSX.thumb"; break; default: thumbPath = MvcApplication.GetCacheDirectory() + "\\" + "PDF.thumb"; break; } if (System.IO.File.Exists(thumbPath)) { byte[] content = null; // getting thumbnail from cache try { using (FileStream fileStream = new FileStream(thumbPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { content = new byte[fileStream.Length]; fileStream.Read(content, 0, (int)fileStream.Length); // safe cast as thumbnail size will never exceed int.maxValue } } catch // can fail during first concurrent accesses. { goto gen_thumb; } return(File(content, "image/png")); } gen_thumb: // GdPicture14.DocumentFormat documentFormat = GdPicture14.DocumentFormat.DocumentFormatUNKNOWN; // GdPicture14.DocumentFormat documentFormat = GdPicture14.DocumentFormat.DocumentFormatDOCX;// h.GetExtension(doc); int thumbnailId = 0; int pageCount = 0; GdPictureStatus status = GdPictureDocumentUtilities.GetDocumentPreview(docPath, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, thumbnailBackgroundColor.ToArgb(), ref documentFormat, ref thumbnailId, ref pageCount); if (status == GdPictureStatus.OK) { using (GdPictureImaging gdpictureImaging = new GdPictureImaging()) { using (MemoryStream memoryStream = new MemoryStream()) { try { if (gdpictureImaging.SaveAsStream(thumbnailId, memoryStream, GdPicture14.DocumentFormat.DocumentFormatTXT, 6) == GdPictureStatus.OK) { byte[] content = memoryStream.ToArray(); // let's cache result using (FileStream fileStream = new FileStream(thumbPath, FileMode.CreateNew, FileAccess.Write, FileShare.None)) { fileStream.Write(content, 0, content.Length); } } } finally { GdPictureDocumentUtilities.DisposeImage(thumbnailId); } } } } } return(null); }