DynamicImageManagerColumn.cs 11 KB

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