/******************************************************************************* * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential * 类的信息: * 1.程序名称:F_MST_GoodsImageAdd.cs * 2.功能描述:产品线性图新建编辑 * 编辑履历: * 作者 日期 版本 修改内容 * 冯林勇 2024-04-15 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing.Printing; using System.Reflection; using System.Windows.Forms; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Client.CommonModule; using Dongke.IBOSS.PRD.WCF.DataModels; using Dongke.IBOSS.PRD.WCF.Proxys; namespace Dongke.IBOSS.PRD.Client.Controls { public partial class F_MST_GoodsImageAdd : DKFormBase { #region 成员变量 string winStatus = String.Empty; string winmatnr = string.Empty; // 产品图片二进制集合 private List _picByteList = new List(); // 产品图片缩略图二进制集合 private List _smallByteList = new List(); // 产品图片实体集合 private List _imgList = new List(); // 已存在的图片实体集合(修改) private List _updateImgList = new List(); #endregion #region 构造函数 public F_MST_GoodsImageAdd(string Status, string matnr) { InitializeComponent(); winStatus = Status; winmatnr = matnr; } #endregion #region 属性 #endregion #region 控件事件 /// /// 取消 /// /// /// private void btnCancel_Click(object sender, System.EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } /// /// 画面打开 /// /// /// private void F_MST_GoodsImageAdd_Load(object sender, EventArgs e) { try { if (winStatus == "Edit") { ClientRequestEntity cre = new ClientRequestEntity(); cre.NameSpace = "F_MST_GoodsImage"; cre.Name = "getGoodsImage"; cre.Properties["MATNR"] = winmatnr; ServiceResultEntity sre = DoAsync(() => { return SystemModuleProxy.Service.DoRequest(cre); }); if (sre.Status == Constant.ServiceResultStatus.Success) { // 查询成功 if (sre.Data.Tables[0].Rows.Count > 0) { //this.picGoodsImage.Image = ""; } } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 确定方法 /// /// /// private void btnSave_Click(object sender, EventArgs e) { try { //非空校验 if (txtMATNR.Text.ToString() == "" && string.IsNullOrEmpty(txtMATNR.ToString())) { MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "物料编码"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; } if (_picByteList.Count <=0) { MessageBox.Show(string.Format(Messages.MSG_CMN_W005, "线性图"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); return; } if (winStatus == "Edit") { ClientRequestEntity cre = new ClientRequestEntity(); cre.NameSpace = "F_MST_GoodsImage"; cre.Name = "EditGoodsImage"; cre.Properties["MATNR"] = winmatnr; ServiceResultEntity sre = DoAsync(() => { return SystemModuleProxy.Service.DoRequest(cre); }); if (sre.Status == Constant.ServiceResultStatus.Success) { } } else { ServiceResultEntity sre = DoAsync(() => { ClientRequestEntity cre = new ClientRequestEntity(); cre.NameSpace = "F_MST_GoodsImage"; cre.Name = "AddGoodsImage"; cre.Properties["MATNR"] = this.txtMATNR.Text.ToString(); cre.Properties["IMAGE"] = _picByteList[0]; byte[] s = (byte[])cre.Properties["IMAGE"]; return SystemModuleProxy.Service.DoRequest(cre); }); if (sre.Status == Constant.ServiceResultStatus.Success) { MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "线性图", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(string.Format(Messages.E_CMN_S_001, "线性图"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), MethodBase.GetCurrentMethod().Name, this.Text, ex); } } #endregion #region 重写方法 #endregion #region 私有方法/函数 /// /// 上传缩略图 /// private void btnUpload_Click(object sender, EventArgs e) { try { odlgFile.Filter = Constant.FILTER_PIC; odlgFile.FilterIndex = Constant.INT_IS_ZERO; odlgFile.RestoreDirectory = true; odlgFile.Title = "选择产品图片"; odlgFile.FileName = null; odlgFile.RestoreDirectory = true; if (odlgFile.ShowDialog() == DialogResult.OK) { FileInfo file = new FileInfo(odlgFile.FileName); if (Constant.UPLOAD_PIC_MAX_SIZE < file.Length) { MessageBox.Show(string.Format(Messages.MSG_CMN_W013, "产品图片", "大小", "1M"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { Image PicImage = Image.FromStream(file.OpenRead()); this._picByteList.Add(ImageToByte(PicImage)); GetThumbnail(file); //BindImg(); this.picGoodsImage.BackgroundImage = PicImage; } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 将图片文件转换成二进制 /// /// 需要转换的图片 /// 图片二进制数据 private static byte[] ImageToByte(Image img) { try { byte[] smallbuffer = null; using (MemoryStream ms = new MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); ms.Position = Constant.INT_IS_ZERO; smallbuffer = new byte[ms.Length]; ms.Read(smallbuffer, Constant.INT_IS_ZERO, Convert.ToInt32(ms.Length)); ms.Flush(); } return smallbuffer; } catch (Exception ex) { throw ex; } } /// /// 绑定缩略图到控件中 /// private void BindImg() { try { //每次绑定要清空数据源 this.ilPic.Images.Clear(); //将缩略图二进制集合中的数据转换成图片文件 List LSImageList = new List(); foreach (byte[] smallby in _smallByteList) { LSImageList.Add(byteArrayToImage(smallby)); } //添加数据源 foreach (Image img in LSImageList) { ilPic.Images.Add(img); } this.ilPic.ImageSize = new Size(Constant.INT_IS_HUNDRED, Constant.INT_IS_HUNDRED); this.lvPic.LargeImageList = this.ilPic; this.lvPic.BeginUpdate(); //清空列表的数据源 //lvPic.Items.Clear(); //添加列表的数据源 for (int i = 0; i < ilPic.Images.Count; i++) { ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = i; this.lvPic.Items.Add(lvi); } this.lvPic.EndUpdate(); } catch (Exception ex) { throw ex; } } /// /// 重绘缩略图并把缩略图转为二进制储存在集合众 /// /// 图片实体 /// private void GetThumbnail(FileInfo sourceFile) { try { Image imgSource = Image.FromStream(sourceFile.OpenRead()); ImageFormat thisFormat = imgSource.RawFormat; int sW = Constant.INT_IS_ZERO, sH = Constant.INT_IS_ZERO; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height; int destWidth = Constant.INT_IS_HUNDRED; int destHeight = getSmallImageHeight(sWidth, sHeight, destWidth); if (sHeight > destHeight || sWidth > destWidth) { if ((sWidth * destHeight) > (sHeight * destWidth)) { sW = destWidth; sH = (destWidth * sHeight) / sWidth; } else { sH = destHeight; sW = (sWidth * destHeight) / sHeight; } } else { sW = sWidth; sH = sHeight; } Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Black); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / Constant.INT_IS_TWO, (destHeight - sH) / Constant.INT_IS_TWO, sW, sH), Constant.INT_IS_ZERO, Constant.INT_IS_ZERO, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); //将重绘的图片转为二进制并保存 Image image = (Image)outBmp; byte[] smallbuffer = ImageToByte(image); this._smallByteList.Add(smallbuffer); imgSource.Dispose(); outBmp.Dispose(); } catch (Exception ex) { throw ex; } } /// /// 将数据库中的二进制转换成图片 /// /// 需要转换成图片的二进制数据 /// 图片文件实体 private static Image byteArrayToImage(object data) { try { System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])data); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); return returnImage; } catch (Exception ex) { throw ex; } } /// /// 根据原图片宽高比获取缩略图的高(根据宽) /// /// 原图宽度 /// 原图高度 /// 缩略图宽度 /// 缩略图的高度 private int getSmallImageHeight(int BigWidth, int BigHeight, int SmallWidth) { try { decimal scale = Convert.ToDecimal(BigWidth) / Convert.ToDecimal(BigHeight); return Convert.ToInt32(SmallWidth / scale); } catch (Exception ex) { throw ex; } } #endregion } }