DynamicImageColumn.cs 10 KB

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