DynamicImageManagerColumn.cs 10.0 KB

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