| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /*******************************************************************************
- * 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<byte[]> _picByteList = new List<byte[]>();
- // 产品图片缩略图二进制集合
- private List<byte[]> _smallByteList = new List<byte[]>();
- // 产品图片实体集合
- private List<GoodsImageEntity> _imgList = new List<GoodsImageEntity>();
- // 已存在的图片实体集合(修改)
- private List<GoodsImageEntity> _updateImgList = new List<GoodsImageEntity>();
- #endregion
- #region 构造函数
- public F_MST_GoodsImageAdd(string Status, string matnr)
- {
- InitializeComponent();
- winStatus = Status;
- winmatnr = matnr;
- }
- #endregion
- #region 属性
- #endregion
- #region 控件事件
- /// <summary>
- /// 取消
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCancel_Click(object sender, System.EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- this.Close();
- }
- /// <summary>
- /// 画面打开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void F_MST_GoodsImageAdd_Load(object sender, EventArgs e)
- {
- try
- {
- if (winStatus == "Edit")
- {
- this.btnUpload.Visible = false;
- this.btnSave.Visible = false;
- ClientRequestEntity cre = new ClientRequestEntity();
- cre.NameSpace = "F_MST_GoodsImage";
- cre.Name = "getGoodsImage";
- cre.Properties["MATNR"] = winmatnr;
- ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
- {
- return SystemModuleProxy.Service.DoRequest(cre);
- });
- if (sre.Status == Constant.ServiceResultStatus.Success)
- {
- // 查询成功
- if (sre.Data.Tables[0].Rows.Count > 0)
- {
- this.txtMATNR.Text = winmatnr;
- this.txtMATNR.Enabled = false;
- // 读取二进制数据
- byte[] binaryData = (byte[])(sre.Data.Tables[0].Rows[0]["IMAGE"]);
- // 解码为图像对象
- try
- {
- using (MemoryStream stream = new MemoryStream(binaryData))
- {
- //if (picGoodsImage.Image != null)
- {
- // 其他操作
- // 显示图像在PictureBox中
- //picGoodsImage.Image = Image.FromStream(stream);
- Image PicImage = Image.FromStream(stream);
- this._picByteList.Add(ImageToByte(PicImage));
- this.picGoodsImage.BackgroundImage = PicImage;
- }
- }
- }
- catch (ArgumentException ex)
- {
- // 处理异常,例如记录日志或显示用户友好的错误消息
- MessageBox.Show("加载失败: " + ex.Message);
- }
- //this.picGoodsImage.Image = "";
- }
- }
- }
- }
- catch (Exception ex)
- {
- // 对异常进行共通处理
- ExceptionManager.HandleEventException(this.ToString(),
- MethodBase.GetCurrentMethod().Name, this.Text, ex);
- }
- }
- /// <summary>
- /// 确定方法
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<ServiceResultEntity>(() =>
- {
- return SystemModuleProxy.Service.DoRequest(cre);
- });
- if (sre.Status == Constant.ServiceResultStatus.Success)
- {
- }
- }
- else
- {
- ServiceResultEntity sre = DoAsync<ServiceResultEntity>(() =>
- {
- 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 私有方法/函数
- /// <summary>
- /// 上传缩略图
- /// </summary>
- private void btnUpload_Click(object sender, EventArgs e)
- {
- try
- {
- odlgFile.Filter = Constant.FILTER_PIC_GOODS;
- 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);
- }
- }
- /// <summary>
- /// 将图片文件转换成二进制
- /// </summary>
- /// <param name="img">需要转换的图片</param>
- /// <returns>图片二进制数据</returns>
- 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;
- }
- }
- /// <summary>
- /// 绑定缩略图到控件中
- /// </summary>
- private void BindImg()
- {
- try
- {
- //每次绑定要清空数据源
- this.ilPic.Images.Clear();
- //将缩略图二进制集合中的数据转换成图片文件
- List<Image> LSImageList = new List<Image>();
- 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;
- }
- }
- /// <summary>
- /// 重绘缩略图并把缩略图转为二进制储存在集合众
- /// </summary>
- /// <param name="sourceFile">图片实体</param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 将数据库中的二进制转换成图片
- /// </summary>
- /// <param name="data">需要转换成图片的二进制数据</param>
- /// <returns>图片文件实体</returns>
- 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;
- }
- }
- /// <summary>
- /// 根据原图片宽高比获取缩略图的高(根据宽)
- /// </summary>
- /// <param name="BigWidth">原图宽度</param>
- /// <param name="BigHeight">原图高度</param>
- /// <param name="SmallWidth">缩略图宽度</param>
- /// <returns>缩略图的高度</returns>
- 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
- }
- }
|