using Comal.Stores; using InABox.Core; using System.Linq; using System.IO; using Syncfusion.Pdf.Parsing; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Media.Imaging; using InABox.WPF; using NPOI.SS.Util; using ImageUtils = InABox.WPF.ImageUtils; using System; namespace PRSStores { //In order to have BeforeSave active for a specific EntityDocument, it has to be concretized from this abstract class public abstract class EntityDocumentStore : BaseStore where TEntityDocument : EntityDocument, new() where TEntityLink : EntityLink, new() where TEntity : Entity, new() { protected override void BeforeSave(TEntityDocument entity) { SaveThumbnail(entity); //current size for saved thumbnails is 256*256 base.BeforeSave(entity); } private void SaveThumbnail(TEntityDocument entity) { if (entity.DocumentLink.ID == Guid.Empty) return; if (entity.Thumbnail?.Any() == true) return; CoreTable table = FindSubStore().Query(new Filter(x => x.ID).IsEqualTo(entity.DocumentLink.ID), Columns.None().Add(x => x.Data, x => x.FileName)); if (table.Rows.Count == 0) return; Document doc = table.Rows.FirstOrDefault().ToObject(); if (doc.Data?.Any() != true) return; if (doc.FileName.ToLower().EndsWith("pdf")) { try { PdfLoadedDocument loadeddoc = new PdfLoadedDocument(doc.Data); Bitmap image = loadeddoc.ExportAsImage(0, new SizeF(256, 256), true); MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); entity.Thumbnail = stream.ToArray(); } catch (Exception ex) { Logger.Send(LogType.Information, UserID, ex.Message); } } else { try { var bmp = ImageUtils.BitmapImageFromBytes(doc.Data); bmp = bmp.Scale(256, 256); entity.Thumbnail = bmp.ToArray(); } catch (Exception ex) { Logger.Send(LogType.Information, UserID, ex.Message); } } } } }