DynamicImageManagerColumn.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Threading;
  11. using InABox.Clients;
  12. using InABox.Core;
  13. using InABox.Dxf;
  14. using InABox.WPF;
  15. using Microsoft.Win32;
  16. namespace InABox.DynamicGrid
  17. {
  18. public class DynamicImagePreviewColumn<T> : DynamicImageColumn
  19. where T : BaseObject
  20. {
  21. private static readonly BitmapImage _preview = Wpf.Resources.doc_png.AsBitmapImage();
  22. protected static Expression<Func<T, ImageDocumentLink>> Property { get; private set; }
  23. protected static string ImageIDProperty => Property != null ? CoreUtils.GetFullPropertyName(Property, ".") + ".ID" : "";
  24. public DynamicImagePreviewColumn(Expression<Func<T, ImageDocumentLink>> property) : base(PreviewImage)
  25. {
  26. Property = property;
  27. Image = PreviewImage;
  28. Filters = new[] { "Image Present", "Blank Image" };
  29. FilterRecord = DoFilterImages;
  30. ToolTip = CreateImageToolTip;
  31. }
  32. private static BitmapImage? PreviewImage(CoreRow? arg)
  33. {
  34. if (arg is null || arg.Get<Guid>(ImageIDProperty) == Guid.Empty)
  35. return null;
  36. return _preview;
  37. }
  38. protected Bitmap? LoadBitmapFromDatabase(Guid imageid)
  39. {
  40. if (imageid == Guid.Empty)
  41. return null;
  42. Bitmap? result = null;
  43. var image = new Client<Document>().Query(
  44. new Filter<Document>(x => x.ID).IsEqualTo(imageid),
  45. new Columns<Document>(x => x.ID, x => x.Data)
  46. ).Rows.FirstOrDefault();
  47. if (image != null)
  48. {
  49. var ms = new MemoryStream(image.Get<Document, byte[]>(x => x.Data));
  50. result = new Bitmap(ms);
  51. }
  52. return result;
  53. }
  54. private FrameworkElement? CreateImageToolTip(DynamicActionColumn column, CoreRow? arg)
  55. {
  56. if (arg is null)
  57. return null;
  58. FrameworkElement? result = null;
  59. var imageid = arg.Get<Guid>(ImageIDProperty);
  60. if (!imageid.Equals(Guid.Empty))
  61. using (new WaitCursor())
  62. {
  63. var bmp = LoadBitmapFromDatabase(imageid);
  64. result = ImageToolTip(bmp?.AsBitmapImage(false));
  65. }
  66. return result;
  67. }
  68. private bool DoFilterImages(CoreRow row, string[] filter)
  69. {
  70. var hasimage = row.Get<Guid>(ImageIDProperty) != Guid.Empty;
  71. if (filter.Contains("Image Present") && hasimage)
  72. return true;
  73. if (filter.Contains("Blank Image") && !hasimage)
  74. return true;
  75. return false;
  76. }
  77. }
  78. public class DynamicImageManagerColumn<T> : DynamicImagePreviewColumn<T>
  79. where T : Entity, IRemotable, IPersistent, new()
  80. {
  81. private readonly bool _canupdate;
  82. private readonly IDynamicGrid _parent;
  83. private static string _imagefilename => Property != null ? CoreUtils.GetFullPropertyName(Property, ".") + ".FileName" : "";
  84. public DynamicImageManagerColumn(IDynamicGrid parent, Expression<Func<T, ImageDocumentLink>> property, bool canupdate) : base(property)
  85. {
  86. _parent = parent;
  87. _canupdate = canupdate;
  88. ContextMenu = CreateImageMenu;
  89. }
  90. private MenuItem CreateMenu(string caption, RoutedEventHandler click)
  91. {
  92. var item = new MenuItem();
  93. item.Header = caption;
  94. item.Click += click;
  95. return item;
  96. }
  97. private void SaveBitmapToDatabase(CoreRow row, Guid id, string filename, Bitmap? bitmap)
  98. {
  99. var docid = id;
  100. if (bitmap != null && docid == Guid.Empty)
  101. {
  102. byte[] data;
  103. using (var ms = new MemoryStream())
  104. {
  105. bitmap.Save(ms, ImageFormat.Png);
  106. data = ms.GetBuffer();
  107. }
  108. var crc = CoreUtils.CalculateCRC(data);
  109. var doc = new Client<Document>().Query(
  110. new Filter<Document>(x => x.FileName).IsEqualTo(Path.GetFileName(filename)).And(x => x.CRC).IsEqualTo(crc),
  111. new Columns<Document>(x => x.ID)
  112. ).Rows.FirstOrDefault()?.ToObject<Document>();
  113. if (doc == null)
  114. {
  115. doc = new Document
  116. {
  117. FileName = Path.GetFileName(filename),
  118. CRC = crc,
  119. TimeStamp = DateTime.Now,
  120. Data = data
  121. };
  122. new Client<Document>().Save(doc, "");
  123. }
  124. docid = doc.ID;
  125. }
  126. var item = row.ToObject<T>();
  127. CoreUtils.SetPropertyValue(item, ImageIDProperty, docid);
  128. CoreUtils.SetPropertyValue(item, _imagefilename, Path.GetFileName(filename));
  129. new Client<T>().Save(item, "", (p, err) => { });
  130. // False here to prevent Refreshing and losing the selected row record
  131. _parent.UpdateRow(row, ImageIDProperty, docid, false);
  132. _parent.UpdateRow(row, _imagefilename, Path.GetFileName(filename));
  133. }
  134. private ContextMenu? CreateImageMenu(CoreRow[]? rows)
  135. {
  136. if (rows == null || rows.Length != 1)
  137. return null;
  138. var hasimage = rows[0].Get<Guid>(ImageIDProperty) != Guid.Empty;
  139. var canpaste = (_canupdate && Clipboard.ContainsData("ProductImage")) || Clipboard.ContainsImage();
  140. var result = new ContextMenu();
  141. if (_canupdate)
  142. result.Items.Add(CreateMenu("Load From File", (o, e) => LoadImage(rows[0])));
  143. if (hasimage)
  144. result.Items.Add(CreateMenu("Save To File", (o, e) => SaveImage(rows[0])));
  145. if (hasimage || canpaste)
  146. result.Items.Add(new Separator());
  147. if (hasimage)
  148. result.Items.Add(CreateMenu("Copy To Clipboard", (o, e) => CopyImage(rows[0])));
  149. if (canpaste)
  150. result.Items.Add(CreateMenu("Paste From Clipboard", (o, e) => PasteImage(rows[0])));
  151. if (_canupdate && hasimage)
  152. {
  153. result.Items.Add(new Separator());
  154. result.Items.Add(CreateMenu("Clear Image", (o, e) => ClearImage(rows[0])));
  155. }
  156. return result;
  157. }
  158. private void LoadImage(CoreRow row)
  159. {
  160. var dlg = new OpenFileDialog();
  161. dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
  162. if (dlg.ShowDialog() == true)
  163. using (new WaitCursor())
  164. {
  165. var filename = dlg.FileName.ToLower();
  166. Bitmap? bmp;
  167. if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
  168. {
  169. DxfUtils.OnProcessError += ((message) =>
  170. {
  171. MessageBox.Show("Error - " + message);
  172. });
  173. bmp = DxfUtils.DXFToBitmap(filename);
  174. }
  175. else
  176. bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
  177. SaveBitmapToDatabase(row, Guid.Empty, filename, bmp);
  178. //_parent?.Refresh(false, false);
  179. }
  180. }
  181. private void SaveImage(CoreRow row)
  182. {
  183. var imageid = row.Get<Guid>(ImageIDProperty);
  184. if (imageid == Guid.Empty)
  185. return;
  186. var filename = row.Get<string>(_imagefilename);
  187. var dlg = new SaveFileDialog();
  188. dlg.Filter = "Image Files (*.png)|*.png";
  189. dlg.FileName = filename;
  190. if (dlg.ShowDialog() == true)
  191. {
  192. var bmp = LoadBitmapFromDatabase(imageid);
  193. bmp?.Save(dlg.FileName);
  194. }
  195. }
  196. private void CopyImage(CoreRow row)
  197. {
  198. var bmp = LoadBitmapFromDatabase(row.Get<Guid>(ImageIDProperty));
  199. var data = new Tuple<Guid, string, Bitmap?>(
  200. row.Get<Guid>(ImageIDProperty),
  201. row.Get<string>(_imagefilename),
  202. bmp
  203. );
  204. Clipboard.SetData("ProductImage", data);
  205. }
  206. private void PasteImage(CoreRow row)
  207. {
  208. if (row.Get<Guid>(ImageIDProperty) != Guid.Empty)
  209. if (MessageBox.Show("Are you sure you wish to update the image for the selected items(s)?", "Update Images", MessageBoxButton.YesNo,
  210. MessageBoxImage.Question) != MessageBoxResult.Yes)
  211. return;
  212. var id = Guid.Empty;
  213. var filename = "";
  214. Bitmap? bitmap = null;
  215. if (Clipboard.ContainsData("ProductImage") && Clipboard.GetData("ProductImage") is Tuple<Guid, string, Bitmap?> data)
  216. {
  217. id = data.Item1;
  218. filename = data.Item2;
  219. bitmap = data.Item3;
  220. }
  221. else if (Clipboard.ContainsImage())
  222. {
  223. var dataImage = Clipboard.GetImage();
  224. bitmap = dataImage.AsBitmap2();
  225. filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
  226. if (Clipboard.ContainsFileDropList())
  227. {
  228. var list = Clipboard.GetFileDropList();
  229. if (list.Count > 0)
  230. filename = Path.ChangeExtension(Path.GetFileName(list[0]!), ".png");
  231. }
  232. //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
  233. //bitmap.Save(filename);
  234. id = Guid.Empty;
  235. }
  236. if (bitmap is null)
  237. {
  238. MessageBox.Show("Unable to paste data from clipboard");
  239. return;
  240. }
  241. using (new WaitCursor())
  242. {
  243. SaveBitmapToDatabase(row, id, filename, bitmap);
  244. }
  245. //_parent?.Refresh(false, false);
  246. }
  247. private void ClearImage(CoreRow row)
  248. {
  249. if (MessageBox.Show("Are you sure you wish to clear the image for the selected product(s)?", "Clear Images", MessageBoxButton.YesNo,
  250. MessageBoxImage.Question) != MessageBoxResult.Yes)
  251. return;
  252. using (new WaitCursor())
  253. {
  254. SaveBitmapToDatabase(row, Guid.Empty, "", null);
  255. }
  256. //_parent?.Refresh(false, false);
  257. }
  258. }
  259. }