F_MST_GoodsImageAdd.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:F_MST_GoodsImageAdd.cs
  5. * 2.功能描述:产品线性图新建编辑
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 冯林勇 2024-04-15 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing.Printing;
  15. using System.Reflection;
  16. using System.Windows.Forms;
  17. using System.IO;
  18. using System.Drawing;
  19. using System.Drawing.Drawing2D;
  20. using System.Drawing.Imaging;
  21. using Dongke.IBOSS.PRD.Basics.BaseResources;
  22. using Dongke.IBOSS.PRD.Client.CommonModule;
  23. using Dongke.IBOSS.PRD.WCF.DataModels;
  24. using Dongke.IBOSS.PRD.WCF.Proxys;
  25. namespace Dongke.IBOSS.PRD.Client.Controls
  26. {
  27. public partial class F_MST_GoodsImageAdd : DKFormBase
  28. {
  29. #region 成员变量
  30. string winStatus = String.Empty;
  31. string winmatnr = string.Empty;
  32. // 产品图片二进制集合
  33. private List<byte[]> _picByteList = new List<byte[]>();
  34. // 产品图片缩略图二进制集合
  35. private List<byte[]> _smallByteList = new List<byte[]>();
  36. // 产品图片实体集合
  37. private List<GoodsImageEntity> _imgList = new List<GoodsImageEntity>();
  38. // 已存在的图片实体集合(修改)
  39. private List<GoodsImageEntity> _updateImgList = new List<GoodsImageEntity>();
  40. #endregion
  41. #region 构造函数
  42. public F_MST_GoodsImageAdd(string Status, string matnr)
  43. {
  44. InitializeComponent();
  45. winStatus = Status;
  46. winmatnr = matnr;
  47. }
  48. #endregion
  49. #region 属性
  50. #endregion
  51. #region 控件事件
  52. /// <summary>
  53. /// 取消
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. private void btnCancel_Click(object sender, System.EventArgs e)
  58. {
  59. this.DialogResult = DialogResult.Cancel;
  60. this.Close();
  61. }
  62. /// <summary>
  63. /// 画面打开
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void F_MST_GoodsImageAdd_Load(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. if (winStatus == "Edit")
  72. {
  73. ClientRequestEntity cre = new ClientRequestEntity();
  74. cre.NameSpace = "F_MST_GoodsImage";
  75. cre.Name = "getGoodsImage";
  76. cre.Properties["MATNR"] = winmatnr;
  77. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  78. {
  79. return SystemModuleProxy.Service.DoRequest(cre);
  80. });
  81. if (sre.Status == Constant.ServiceResultStatus.Success)
  82. {
  83. // 查询成功
  84. if (sre.Data.Tables[0].Rows.Count > 0)
  85. {
  86. //this.picGoodsImage.Image = "";
  87. }
  88. }
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. // 对异常进行共通处理
  94. ExceptionManager.HandleEventException(this.ToString(),
  95. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  96. }
  97. }
  98. /// <summary>
  99. /// 确定方法
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. private void btnSave_Click(object sender, EventArgs e)
  104. {
  105. try
  106. {
  107. //非空校验
  108. if (txtMATNR.Text.ToString() == "" && string.IsNullOrEmpty(txtMATNR.ToString()))
  109. {
  110. MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "物料编码"),
  111. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
  112. return;
  113. }
  114. if (_picByteList.Count <=0)
  115. {
  116. MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "线性图"),
  117. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
  118. return;
  119. }
  120. if (winStatus == "Edit")
  121. {
  122. ClientRequestEntity cre = new ClientRequestEntity();
  123. cre.NameSpace = "F_MST_GoodsImage";
  124. cre.Name = "EditGoodsImage";
  125. cre.Properties["MATNR"] = winmatnr;
  126. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  127. {
  128. return SystemModuleProxy.Service.DoRequest(cre);
  129. });
  130. if (sre.Status == Constant.ServiceResultStatus.Success)
  131. {
  132. }
  133. }
  134. else
  135. {
  136. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  137. {
  138. ClientRequestEntity cre = new ClientRequestEntity();
  139. cre.NameSpace = "F_MST_GoodsImage";
  140. cre.Name = "AddGoodsImage";
  141. cre.Properties["MATNR"] = this.txtMATNR.Text.ToString();
  142. cre.Properties["IMAGE"] = _picByteList[0];
  143. byte[] s = (byte[])cre.Properties["IMAGE"];
  144. return SystemModuleProxy.Service.DoRequest(cre);
  145. });
  146. if (sre.Status == Constant.ServiceResultStatus.Success)
  147. {
  148. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "线性图", "保存"),
  149. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  150. }
  151. else
  152. {
  153. MessageBox.Show(string.Format(Messages.E_CMN_S_001, "线性图"), this.Text,
  154. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. // 对异常进行共通处理
  161. ExceptionManager.HandleEventException(this.ToString(),
  162. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  163. }
  164. }
  165. #endregion
  166. #region 重写方法
  167. #endregion
  168. #region 私有方法/函数
  169. /// <summary>
  170. /// 上传缩略图
  171. /// </summary>
  172. private void btnUpload_Click(object sender, EventArgs e)
  173. {
  174. try
  175. {
  176. odlgFile.Filter = Constant.FILTER_PIC;
  177. odlgFile.FilterIndex = Constant.INT_IS_ZERO;
  178. odlgFile.RestoreDirectory = true;
  179. odlgFile.Title = "选择产品图片";
  180. odlgFile.FileName = null;
  181. odlgFile.RestoreDirectory = true;
  182. if (odlgFile.ShowDialog() == DialogResult.OK)
  183. {
  184. FileInfo file = new FileInfo(odlgFile.FileName);
  185. if (Constant.UPLOAD_PIC_MAX_SIZE < file.Length)
  186. {
  187. MessageBox.Show(string.Format(Messages.MSG_CMN_W013, "产品图片", "大小", "1M"),
  188. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  189. }
  190. else
  191. {
  192. Image PicImage = Image.FromStream(file.OpenRead());
  193. this._picByteList.Add(ImageToByte(PicImage));
  194. GetThumbnail(file);
  195. //BindImg();
  196. this.picGoodsImage.BackgroundImage = PicImage;
  197. }
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. // 对异常进行共通处理
  203. ExceptionManager.HandleEventException(this.ToString(),
  204. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  205. }
  206. }
  207. /// <summary>
  208. /// 将图片文件转换成二进制
  209. /// </summary>
  210. /// <param name="img">需要转换的图片</param>
  211. /// <returns>图片二进制数据</returns>
  212. private static byte[] ImageToByte(Image img)
  213. {
  214. try
  215. {
  216. byte[] smallbuffer = null;
  217. using (MemoryStream ms = new MemoryStream())
  218. {
  219. img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  220. ms.Position = Constant.INT_IS_ZERO;
  221. smallbuffer = new byte[ms.Length];
  222. ms.Read(smallbuffer, Constant.INT_IS_ZERO, Convert.ToInt32(ms.Length));
  223. ms.Flush();
  224. }
  225. return smallbuffer;
  226. }
  227. catch (Exception ex)
  228. {
  229. throw ex;
  230. }
  231. }
  232. /// <summary>
  233. /// 绑定缩略图到控件中
  234. /// </summary>
  235. private void BindImg()
  236. {
  237. try
  238. {
  239. //每次绑定要清空数据源
  240. this.ilPic.Images.Clear();
  241. //将缩略图二进制集合中的数据转换成图片文件
  242. List<Image> LSImageList = new List<Image>();
  243. foreach (byte[] smallby in _smallByteList)
  244. {
  245. LSImageList.Add(byteArrayToImage(smallby));
  246. }
  247. //添加数据源
  248. foreach (Image img in LSImageList)
  249. {
  250. ilPic.Images.Add(img);
  251. }
  252. this.ilPic.ImageSize = new Size(Constant.INT_IS_HUNDRED, Constant.INT_IS_HUNDRED);
  253. this.lvPic.LargeImageList = this.ilPic;
  254. this.lvPic.BeginUpdate();
  255. //清空列表的数据源
  256. //lvPic.Items.Clear();
  257. //添加列表的数据源
  258. for (int i = 0; i < ilPic.Images.Count; i++)
  259. {
  260. ListViewItem lvi = new ListViewItem();
  261. lvi.ImageIndex = i;
  262. this.lvPic.Items.Add(lvi);
  263. }
  264. this.lvPic.EndUpdate();
  265. }
  266. catch (Exception ex)
  267. {
  268. throw ex;
  269. }
  270. }
  271. /// <summary>
  272. /// 重绘缩略图并把缩略图转为二进制储存在集合众
  273. /// </summary>
  274. /// <param name="sourceFile">图片实体</param>
  275. /// <returns></returns>
  276. private void GetThumbnail(FileInfo sourceFile)
  277. {
  278. try
  279. {
  280. Image imgSource = Image.FromStream(sourceFile.OpenRead());
  281. ImageFormat thisFormat = imgSource.RawFormat;
  282. int sW = Constant.INT_IS_ZERO, sH = Constant.INT_IS_ZERO;
  283. // 按比例缩放
  284. int sWidth = imgSource.Width;
  285. int sHeight = imgSource.Height;
  286. int destWidth = Constant.INT_IS_HUNDRED;
  287. int destHeight = getSmallImageHeight(sWidth, sHeight, destWidth);
  288. if (sHeight > destHeight || sWidth > destWidth)
  289. {
  290. if ((sWidth * destHeight) > (sHeight * destWidth))
  291. {
  292. sW = destWidth;
  293. sH = (destWidth * sHeight) / sWidth;
  294. }
  295. else
  296. {
  297. sH = destHeight;
  298. sW = (sWidth * destHeight) / sHeight;
  299. }
  300. }
  301. else
  302. {
  303. sW = sWidth;
  304. sH = sHeight;
  305. }
  306. Bitmap outBmp = new Bitmap(destWidth, destHeight);
  307. Graphics g = Graphics.FromImage(outBmp);
  308. g.Clear(Color.Black);
  309. // 设置画布的描绘质量
  310. g.CompositingQuality = CompositingQuality.HighQuality;
  311. g.SmoothingMode = SmoothingMode.HighQuality;
  312. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  313. g.DrawImage(imgSource, new Rectangle((destWidth - sW) / Constant.INT_IS_TWO, (destHeight - sH) /
  314. Constant.INT_IS_TWO, sW, sH), Constant.INT_IS_ZERO, Constant.INT_IS_ZERO,
  315. imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
  316. g.Dispose();
  317. //将重绘的图片转为二进制并保存
  318. Image image = (Image)outBmp;
  319. byte[] smallbuffer = ImageToByte(image);
  320. this._smallByteList.Add(smallbuffer);
  321. imgSource.Dispose();
  322. outBmp.Dispose();
  323. }
  324. catch (Exception ex)
  325. {
  326. throw ex;
  327. }
  328. }
  329. /// <summary>
  330. /// 将数据库中的二进制转换成图片
  331. /// </summary>
  332. /// <param name="data">需要转换成图片的二进制数据</param>
  333. /// <returns>图片文件实体</returns>
  334. private static Image byteArrayToImage(object data)
  335. {
  336. try
  337. {
  338. System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])data);
  339. System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
  340. return returnImage;
  341. }
  342. catch (Exception ex)
  343. {
  344. throw ex;
  345. }
  346. }
  347. /// <summary>
  348. /// 根据原图片宽高比获取缩略图的高(根据宽)
  349. /// </summary>
  350. /// <param name="BigWidth">原图宽度</param>
  351. /// <param name="BigHeight">原图高度</param>
  352. /// <param name="SmallWidth">缩略图宽度</param>
  353. /// <returns>缩略图的高度</returns>
  354. private int getSmallImageHeight(int BigWidth, int BigHeight, int SmallWidth)
  355. {
  356. try
  357. {
  358. decimal scale = Convert.ToDecimal(BigWidth) / Convert.ToDecimal(BigHeight);
  359. return Convert.ToInt32(SmallWidth / scale);
  360. }
  361. catch (Exception ex)
  362. {
  363. throw ex;
  364. }
  365. }
  366. #endregion
  367. }
  368. }