/// <summary> /// 压缩文件 /// /// </summary> /// <param name="dir">文件目录</param> /// <param name="zipfilename">zip文件名</param> public void compressFiles(string[] filenames, string zipfilename) { //try //{ // using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename))) // { // s.SetLevel(9); // 0 - store only to 9 - means best compression 压缩等级 // byte[] buffer = new byte[4096]; // foreach (string file in filenames) // { // string elDir = System.IO.Path.GetDirectoryName(file); // string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; // string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; // uint r = WNetConnectionHelper.WNetAddConnection(userName, password, elDir, null); // ZipEntry entry = new ZipEntry(Path.GetFileName(file)); // entry.DateTime = DateTime.Now; // s.PutNextEntry(entry); // using (FileStream fs = File.OpenRead(file)) // { // int sourceBytes; // do // { // sourceBytes = fs.Read(buffer, 0, buffer.Length); // s.Write(buffer, 0, sourceBytes); // } while (sourceBytes > 0); // } // } // s.Finish(); // s.Close(); // } //} //catch(Exception EX) //{ //} System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo; System.Drawing.Imaging.Encoder myEncoder; System.Drawing.Imaging.EncoderParameter myEncoderParameter; System.Drawing.Imaging.EncoderParameters myEncoderParameters; // Get an ImageCodecInfo object that represents the JPEG codec. myImageCodecInfo = GetEncoderInfo("image/jpeg"); // Create an Encoder object based on the GUID // for the Quality parameter category. myEncoder = System.Drawing.Imaging.Encoder.Quality; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1); // Save the bitmap as a JPEG file with 给定的 quality level //压缩等级,0到100,0 最差质量,100 最佳</param> myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, 30L); myEncoderParameters.Param[0] = myEncoderParameter; try { using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename))) { s.SetLevel(9); // 0 - store only to 9 - means best compression 压缩等级 foreach (string file in filenames) { string elDir = System.IO.Path.GetDirectoryName(file); string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; uint r = WNetConnectionHelper.WNetAddConnection(userName, password, elDir, null); ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (MemoryStream ms = new MemoryStream()) { using (System.Drawing.Image img = System.Drawing.Image.FromFile(file)) { img.Save(ms, myImageCodecInfo, myEncoderParameters); byte[] buffer = ms.GetBuffer(); long byteLength = buffer.Length; long leftByterLength = byteLength; int startIndex = 0; int wirteByteLength = 0; while (leftByterLength > 0) { wirteByteLength = leftByterLength >= 4096 ? 4096 : (int)leftByterLength; s.Write(buffer, startIndex, wirteByteLength); leftByterLength = leftByterLength - wirteByteLength; startIndex = startIndex + wirteByteLength; } } } WNetConnectionHelper.WNetCancelConnection(userName, 1, true); } s.Finish(); s.Close(); } } catch (Exception EX) { } }
protected void btnSelect_Click(object sender, EventArgs e) { if (this.ASPTXTPALLETNO.Text.Trim() == "" && this.ASPTXTGUINUM.Text.Trim() == "" && this.ASPMEMOXULIENUM.Text.Trim() == "") { base.ShowMessageBox(this.Page, "出货单号,托号,组件序列号必须输入一项,以防止查询数据量过大,影响系统速度"); return; } string guiNum = this.ASPTXTGUINUM.Text; string palletNum = this.ASPTXTPALLETNO.Text; string xuelieNum = this.ASPMEMOXULIENUM.Text; DataSet ds = wipDownLoadAccess.GetInfByGuiNumPalletNumXuelieNum(guiNum, palletNum, xuelieNum, "", "", ""); DataTable dt = ds.Tables[0]; DataSet dsPath = wipDownLoadAccess.GetPicAddressRootPath(); if (dsPath == null || ds.Tables.Count < 0 || ds.Tables[0].Rows.Count < 0) { //给出提示。 return; } for (int i = 0; i < dt.Rows.Count; i++) { string el = string.Empty; DataRow dr = dt.Rows[i]; DateTime ttime = Convert.ToDateTime(dr["测试时间"]); string lotNo = Convert.ToString(dr["组件序列号"]); //找EL图片 var lnq = from item in dsPath.Tables[0].AsEnumerable() where Convert.ToString(item["PIC_TYPE"]) == "EL" select item; foreach (DataRow drELRootPath in lnq) { string rootPath = Convert.ToString(drELRootPath["PIC_ADDRESS"]); string picDateFormat = Convert.ToString(drELRootPath["PIC_DATE_FORMAT"]); if (string.IsNullOrEmpty(picDateFormat)) { picDateFormat = "yyyy-M-d"; } string picRelationPath = string.Format("{0}\\{1}\\{2}\\{3}.jpg", ttime.ToString("yyyy年"), ttime.ToString("M月"), ttime.ToString(picDateFormat), lotNo.Trim()); string filePath = Path.Combine(rootPath, picRelationPath); string elDir = System.IO.Path.GetDirectoryName(filePath); string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; uint r = WNetConnectionHelper.WNetAddConnection(userName, password, elDir, null); if (File.Exists(filePath)) { dr["EL图片地址"] = filePath; break; } } //找IV测试图片 var lnqIv = from item in dsPath.Tables[0].AsEnumerable() where Convert.ToString(item["PIC_TYPE"]) == "IV" select item; foreach (DataRow drIVRootPath in lnqIv) { string rootPath = Convert.ToString(drIVRootPath["PIC_ADDRESS"]); string picDateFormat = Convert.ToString(drIVRootPath["PIC_DATE_FORMAT"]); if (string.IsNullOrEmpty(picDateFormat)) { picDateFormat = "yyyy-M-d"; } string picRelationPath = string.Format("{0}\\{1}\\{2}\\{3}.gif", ttime.ToString("yyyy年"), ttime.ToString("M月"), ttime.ToString(picDateFormat), lotNo.Trim()); string filePath = Path.Combine(rootPath, picRelationPath); string elDir = System.IO.Path.GetDirectoryName(filePath); string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; uint r = WNetConnectionHelper.WNetAddConnection(userName, password, elDir, null); if (File.Exists(filePath)) { dr["IV测试图片地址"] = filePath; break; } } //找TCard图片(1) var lnqTCard = from item in dsPath.Tables[0].AsEnumerable() where Convert.ToString(item["PIC_TYPE"]) == "TCard" select item; foreach (DataRow drTCardRootPath in lnqTCard) { string rootPath = Convert.ToString(drTCardRootPath["PIC_ADDRESS"]); string picDateFormat = Convert.ToString(drTCardRootPath["PIC_DATE_FORMAT"]); if (string.IsNullOrEmpty(picDateFormat)) { picDateFormat = "yyyy-M-d"; } string picRelationPath1 = string.Format("{0}\\{1}\\{2}\\{3}(1).jpeg", ttime.ToString("yyyy年"), ttime.ToString("M月"), ttime.ToString(picDateFormat), lotNo.Trim()); string picRelationPath2 = string.Format("{0}\\{1}\\{2}\\{3}(2).jpeg", ttime.ToString("yyyy年"), ttime.ToString("M月"), ttime.ToString(picDateFormat), lotNo.Trim()); string filePath1 = Path.Combine(rootPath, picRelationPath1); string filePath2 = Path.Combine(rootPath, picRelationPath2); string elDir1 = System.IO.Path.GetDirectoryName(filePath1); string elDir2 = System.IO.Path.GetDirectoryName(filePath2); string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; uint r1 = WNetConnectionHelper.WNetAddConnection(userName, password, elDir1, null); uint r2 = WNetConnectionHelper.WNetAddConnection(userName, password, elDir2, null); if (File.Exists(filePath1)) { dr["TCard图片地址(1)"] = filePath1; } if (File.Exists(filePath2)) { dr["TCard图片地址(2)"] = filePath2; } } BindData(ds); } if (ds.Tables[0].Rows.Count < 1) { this.grid.DataSource = ds.Tables[0]; this.grid.DataBind(); } this.UpdatePanelResult.Update(); }
/// <summary> /// 压缩文件 /// /// </summary> /// <param name="dir">文件目录</param> /// <param name="zipfilename">zip文件名</param> public void compressFiles(string[] filenames, string zipfilename) { System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo; System.Drawing.Imaging.Encoder myEncoder; System.Drawing.Imaging.EncoderParameter myEncoderParameter; System.Drawing.Imaging.EncoderParameters myEncoderParameters; // Get an ImageCodecInfo object that represents the JPEG codec. myImageCodecInfo = GetEncoderInfo("image/jpeg"); // Create an Encoder object based on the GUID // for the Quality parameter category. myEncoder = System.Drawing.Imaging.Encoder.Quality; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1); // Save the bitmap as a JPEG file with 给定的 quality level //压缩等级,0到100,0 最差质量,100 最佳</param> myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1); myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, 30L); myEncoderParameters.Param[0] = myEncoderParameter; try { using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename))) { s.SetLevel(9); // 0 - store only to 9 - means best compression 压缩等级 foreach (string file in filenames) { string elDir = System.IO.Path.GetDirectoryName(file); string userName = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_username"]; string password = System.Web.Configuration.WebConfigurationManager.AppSettings["el_share_authenticate_password"]; uint r = WNetConnectionHelper.WNetAddConnection(userName, password, elDir, null); ZipEntry entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); //byte[] file1 = File.ReadAllBytes(file); using (MemoryStream ms = new MemoryStream()) { using (System.Drawing.Image img = System.Drawing.Image.FromFile(file)) { byte[] buffer = new byte[1]; if (CheckBox1.Checked == false) { img.Save(ms, myImageCodecInfo, myEncoderParameters); buffer = ms.GetBuffer(); } else if (CheckBox1.Checked == true) { FileInfo MyFileInfo = new FileInfo(file); float MyFileSize = (float)MyFileInfo.Length / 1024; if (MyFileSize <= 650) { buffer = File.ReadAllBytes(file); } else if (MyFileSize > 650 && MyFileSize < 900) { int param = Convert.ToInt32(75 + 600 / MyFileSize * 22); buffer = getThumImage(file, param).ToArray(); } else if (MyFileSize > 900 && MyFileSize < 1100) { int param = Convert.ToInt32(75 + 600 / MyFileSize * 20); buffer = getThumImage(file, param).ToArray(); } else if (1100 < MyFileSize && MyFileSize < 1200) { buffer = File.ReadAllBytes(file); } else if (1200 < MyFileSize && MyFileSize < 1400) { int param = Convert.ToInt32(70 + 600 / MyFileSize * 20); buffer = getThumImage(file, param).ToArray(); } else if (MyFileSize > 1400 && MyFileSize < 1900) { int param = Convert.ToInt32(1900 / MyFileSize * 15); buffer = getThumImage(file, param).ToArray(); } else if (MyFileSize > 1900) { int param = Convert.ToInt32(10 + 1900 / MyFileSize * 10); buffer = getThumImage(file, param).ToArray(); } } long byteLength = buffer.Length; long leftByterLength = byteLength; int startIndex = 0; int wirteByteLength = 0; while (leftByterLength > 0) { wirteByteLength = leftByterLength >= 4096 ? 4096 : (int)leftByterLength; s.Write(buffer, startIndex, wirteByteLength); leftByterLength = leftByterLength - wirteByteLength; startIndex = startIndex + wirteByteLength; } } #region //using (System.Drawing.Image img = System.Drawing.Image.FromFile(file)) //{ // FileInfo MyFileInfo = new FileInfo(file); // float MyFileSize = (float)MyFileInfo.Length / 1024; // if (MyFileSize <= 650) // { // img.Save(ms, ImageFormat.Jpeg); // } // else if (MyFileSize > 650) // { // img.Save(ms, ImageFormat.Jpeg); // } // //img.Save(ms, myImageCodecInfo, myEncoderParameters); // byte[] buffer = ms.GetBuffer(); // long byteLength = buffer.Length; // long leftByterLength = byteLength; // int startIndex = 0; // int wirteByteLength = 0; // while (leftByterLength > 0) // { // wirteByteLength = leftByterLength >= 4096 ? 4096 : (int)leftByterLength; // s.Write(buffer, startIndex, wirteByteLength); // leftByterLength = leftByterLength - wirteByteLength; // startIndex = startIndex + wirteByteLength; // } //} #endregion } WNetConnectionHelper.WNetCancelConnection(userName, 1, true); } s.Finish(); s.Close(); } } catch (Exception EX) { } }