F_MST_GoodsImageAdd.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. this.btnUpload.Visible = false;
  74. this.btnSave.Visible = false;
  75. ClientRequestEntity cre = new ClientRequestEntity();
  76. cre.NameSpace = "F_MST_GoodsImage";
  77. cre.Name = "getGoodsImage";
  78. cre.Properties["MATNR"] = winmatnr;
  79. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  80. {
  81. return SystemModuleProxy.Service.DoRequest(cre);
  82. });
  83. if (sre.Status == Constant.ServiceResultStatus.Success)
  84. {
  85. // 查询成功
  86. if (sre.Data.Tables[0].Rows.Count > 0)
  87. {
  88. this.txtMATNR.Text = winmatnr;
  89. this.txtMATNR.Enabled = false;
  90. // 读取二进制数据
  91. byte[] binaryData = (byte[])(sre.Data.Tables[0].Rows[0]["IMAGE"]);
  92. // 解码为图像对象
  93. try
  94. {
  95. using (MemoryStream stream = new MemoryStream(binaryData))
  96. {
  97. //if (picGoodsImage.Image != null)
  98. {
  99. // 其他操作
  100. // 显示图像在PictureBox中
  101. //picGoodsImage.Image = Image.FromStream(stream);
  102. Image PicImage = Image.FromStream(stream);
  103. this._picByteList.Add(ImageToByte(PicImage));
  104. this.picGoodsImage.BackgroundImage = PicImage;
  105. }
  106. }
  107. }
  108. catch (ArgumentException ex)
  109. {
  110. // 处理异常,例如记录日志或显示用户友好的错误消息
  111. MessageBox.Show("加载失败: " + ex.Message);
  112. }
  113. //this.picGoodsImage.Image = "";
  114. }
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. // 对异常进行共通处理
  121. ExceptionManager.HandleEventException(this.ToString(),
  122. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  123. }
  124. }
  125. /// <summary>
  126. /// 确定方法
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void btnSave_Click(object sender, EventArgs e)
  131. {
  132. try
  133. {
  134. //非空校验
  135. if (txtMATNR.Text.ToString() == "" && string.IsNullOrEmpty(txtMATNR.ToString()))
  136. {
  137. MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "物料编码"),
  138. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
  139. return;
  140. }
  141. if (_picByteList.Count <=0)
  142. {
  143. MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "线性图"),
  144. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
  145. return;
  146. }
  147. if (winStatus == "Edit")
  148. {
  149. ClientRequestEntity cre = new ClientRequestEntity();
  150. cre.NameSpace = "F_MST_GoodsImage";
  151. cre.Name = "EditGoodsImage";
  152. cre.Properties["MATNR"] = winmatnr;
  153. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  154. {
  155. return SystemModuleProxy.Service.DoRequest(cre);
  156. });
  157. if (sre.Status == Constant.ServiceResultStatus.Success)
  158. {
  159. }
  160. }
  161. else
  162. {
  163. ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
  164. {
  165. ClientRequestEntity cre = new ClientRequestEntity();
  166. cre.NameSpace = "F_MST_GoodsImage";
  167. cre.Name = "AddGoodsImage";
  168. cre.Properties["MATNR"] = this.txtMATNR.Text.ToString();
  169. cre.Properties["IMAGE"] = _picByteList[0];
  170. byte[] s = (byte[])cre.Properties["IMAGE"];
  171. return SystemModuleProxy.Service.DoRequest(cre);
  172. });
  173. if (sre.Status == Constant.ServiceResultStatus.Success)
  174. {
  175. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "线性图", "保存"),
  176. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  177. }
  178. else
  179. {
  180. MessageBox.Show(string.Format(Messages.E_CMN_S_001, "线性图"), this.Text,
  181. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  182. }
  183. }
  184. }
  185. catch (Exception ex)
  186. {
  187. // 对异常进行共通处理
  188. ExceptionManager.HandleEventException(this.ToString(),
  189. MethodBase.GetCurrentMethod().Name, this.Text, ex);
  190. }
  191. }
  192. #endregion
  193. #region 重写方法
  194. #endregion
  195. #region 私有方法/函数
  196. /// <summary>
  197. /// 上传缩略图
  198. /// </summary>
  199. private void btnUpload_Click(object sender, EventArgs e)
  200. {
  201. try
  202. {
  203. odlgFile.Filter = Constant.FILTER_PIC_GOODS;
  204. odlgFile.FilterIndex = Constant.INT_IS_ZERO;
  205. odlgFile.RestoreDirectory = true;
  206. odlgFile.Title = "选择产品图片";
  207. odlgFile.FileName = null;
  208. odlgFile.RestoreDirectory = true;
  209. if (odlgFile.ShowDialog() == DialogResult.OK)
  210. {
  211. FileInfo file = new FileInfo(odlgFile.FileName);
  212. if (Constant.UPLOAD_PIC_MAX_SIZE < file.Length)
  213. {
  214. MessageBox.Show(string.Format(Messages.MSG_CMN_W013, "产品图片", "大小", "1M"),
  215. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  216. }
  217. else
  218. {
  219. Image PicImage = Image.FromStream(file.OpenRead());
  220. this._picByteList.Add(ImageToByte(PicImage));
  221. GetThumbnail(file);
  222. //BindImg();
  223. this.picGoodsImage.BackgroundImage = PicImage;
  224. }
  225. }
  226. }
  227. catch (Exception ex)
  228. {
  229. // 对异常进行共通处理
  230. ExceptionManager.HandleEventException(this.ToString(),
  231. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  232. }
  233. }
  234. /// <summary>
  235. /// 将图片文件转换成二进制
  236. /// </summary>
  237. /// <param name="img">需要转换的图片</param>
  238. /// <returns>图片二进制数据</returns>
  239. private static byte[] ImageToByte(Image img)
  240. {
  241. try
  242. {
  243. byte[] smallbuffer = null;
  244. using (MemoryStream ms = new MemoryStream())
  245. {
  246. img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  247. ms.Position = Constant.INT_IS_ZERO;
  248. smallbuffer = new byte[ms.Length];
  249. ms.Read(smallbuffer, Constant.INT_IS_ZERO, Convert.ToInt32(ms.Length));
  250. ms.Flush();
  251. }
  252. return smallbuffer;
  253. }
  254. catch (Exception ex)
  255. {
  256. throw ex;
  257. }
  258. }
  259. /// <summary>
  260. /// 绑定缩略图到控件中
  261. /// </summary>
  262. private void BindImg()
  263. {
  264. try
  265. {
  266. //每次绑定要清空数据源
  267. this.ilPic.Images.Clear();
  268. //将缩略图二进制集合中的数据转换成图片文件
  269. List<Image> LSImageList = new List<Image>();
  270. foreach (byte[] smallby in _smallByteList)
  271. {
  272. LSImageList.Add(byteArrayToImage(smallby));
  273. }
  274. //添加数据源
  275. foreach (Image img in LSImageList)
  276. {
  277. ilPic.Images.Add(img);
  278. }
  279. this.ilPic.ImageSize = new Size(Constant.INT_IS_HUNDRED, Constant.INT_IS_HUNDRED);
  280. this.lvPic.LargeImageList = this.ilPic;
  281. this.lvPic.BeginUpdate();
  282. //清空列表的数据源
  283. //lvPic.Items.Clear();
  284. //添加列表的数据源
  285. for (int i = 0; i < ilPic.Images.Count; i++)
  286. {
  287. ListViewItem lvi = new ListViewItem();
  288. lvi.ImageIndex = i;
  289. this.lvPic.Items.Add(lvi);
  290. }
  291. this.lvPic.EndUpdate();
  292. }
  293. catch (Exception ex)
  294. {
  295. throw ex;
  296. }
  297. }
  298. /// <summary>
  299. /// 重绘缩略图并把缩略图转为二进制储存在集合众
  300. /// </summary>
  301. /// <param name="sourceFile">图片实体</param>
  302. /// <returns></returns>
  303. private void GetThumbnail(FileInfo sourceFile)
  304. {
  305. try
  306. {
  307. Image imgSource = Image.FromStream(sourceFile.OpenRead());
  308. ImageFormat thisFormat = imgSource.RawFormat;
  309. int sW = Constant.INT_IS_ZERO, sH = Constant.INT_IS_ZERO;
  310. // 按比例缩放
  311. int sWidth = imgSource.Width;
  312. int sHeight = imgSource.Height;
  313. int destWidth = Constant.INT_IS_HUNDRED;
  314. int destHeight = getSmallImageHeight(sWidth, sHeight, destWidth);
  315. if (sHeight > destHeight || sWidth > destWidth)
  316. {
  317. if ((sWidth * destHeight) > (sHeight * destWidth))
  318. {
  319. sW = destWidth;
  320. sH = (destWidth * sHeight) / sWidth;
  321. }
  322. else
  323. {
  324. sH = destHeight;
  325. sW = (sWidth * destHeight) / sHeight;
  326. }
  327. }
  328. else
  329. {
  330. sW = sWidth;
  331. sH = sHeight;
  332. }
  333. Bitmap outBmp = new Bitmap(destWidth, destHeight);
  334. Graphics g = Graphics.FromImage(outBmp);
  335. g.Clear(Color.Black);
  336. // 设置画布的描绘质量
  337. g.CompositingQuality = CompositingQuality.HighQuality;
  338. g.SmoothingMode = SmoothingMode.HighQuality;
  339. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  340. g.DrawImage(imgSource, new Rectangle((destWidth - sW) / Constant.INT_IS_TWO, (destHeight - sH) /
  341. Constant.INT_IS_TWO, sW, sH), Constant.INT_IS_ZERO, Constant.INT_IS_ZERO,
  342. imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
  343. g.Dispose();
  344. //将重绘的图片转为二进制并保存
  345. Image image = (Image)outBmp;
  346. byte[] smallbuffer = ImageToByte(image);
  347. this._smallByteList.Add(smallbuffer);
  348. imgSource.Dispose();
  349. outBmp.Dispose();
  350. }
  351. catch (Exception ex)
  352. {
  353. throw ex;
  354. }
  355. }
  356. /// <summary>
  357. /// 将数据库中的二进制转换成图片
  358. /// </summary>
  359. /// <param name="data">需要转换成图片的二进制数据</param>
  360. /// <returns>图片文件实体</returns>
  361. private static Image byteArrayToImage(object data)
  362. {
  363. try
  364. {
  365. System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])data);
  366. System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
  367. return returnImage;
  368. }
  369. catch (Exception ex)
  370. {
  371. throw ex;
  372. }
  373. }
  374. /// <summary>
  375. /// 根据原图片宽高比获取缩略图的高(根据宽)
  376. /// </summary>
  377. /// <param name="BigWidth">原图宽度</param>
  378. /// <param name="BigHeight">原图高度</param>
  379. /// <param name="SmallWidth">缩略图宽度</param>
  380. /// <returns>缩略图的高度</returns>
  381. private int getSmallImageHeight(int BigWidth, int BigHeight, int SmallWidth)
  382. {
  383. try
  384. {
  385. decimal scale = Convert.ToDecimal(BigWidth) / Convert.ToDecimal(BigHeight);
  386. return Convert.ToInt32(SmallWidth / scale);
  387. }
  388. catch (Exception ex)
  389. {
  390. throw ex;
  391. }
  392. }
  393. #endregion
  394. }
  395. }