private void Btn_SelectCatalog(object sender, RoutedEventArgs e) { //选择文件夹目录 System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog(); dialog.Description = "请选择文件路径"; string foldPath = ""; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foldPath = dialog.SelectedPath + @""; this.txt_Catalog.Text = foldPath; } } private void Btn_SaveCatalog(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(this.txt_Catalog.Text)) { MessageBox.Show("请选择目录后保存!"); this.txt_Catalog.Text = AppSettingsHelper.GetItem("filetmp"); return; } if (Directory.Exists(this.txt_Catalog.Text)==false) { MessageBox.Show("选择目录不存在请确认!"); this.txt_Catalog.Text = AppSettingsHelper.GetItem("filetmp"); return; } AppSettingsHelper.UpdateItem("filetmp", this.txt_Catalog.Text); MessageBox.Show("保存成功!"); }
IO操作不停补充
private MemoryStream StreamToMemoryStream(Stream instream) { MemoryStream outstream = new MemoryStream(); const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = instream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } return outstream; } /// <summary> /// Convert Byte[] to Image /// </summary> /// <param name="buffer"></param> /// <returns></returns> private Image BytesToImage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Image image = System.Drawing.Image.FromStream(ms); return image; } /// <summary> /// Convert Byte[] to Image /// </summary> /// <param name="buffer"></param> /// <returns></returns> private MemoryStream BytesToMemoryStream(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); return ms; }
/// <summary> /// 根据路径获取图片 /// </summary> /// <param name="picUrl"></param> /// <returns></returns> private MemoryStream GetImageBypicUrl(string picUrl) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUrl); Stream stream = request.GetResponse().GetResponseStream(); MemoryStream memoryStream = StreamToMemoryStream(stream); return memoryStream; } catch { return null; } } /// <summary> /// 转内存 /// </summary> /// <param name="instream"></param> /// <returns></returns> private MemoryStream StreamToMemoryStream(Stream instream) { MemoryStream outstream = new MemoryStream(); const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = instream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } return outstream; } /// <summary> /// Convert Byte[] to Image /// </summary> /// <param name="buffer"></param> /// <returns></returns> private Image BytesToImage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); Image image = System.Drawing.Image.FromStream(ms); return image; } /// <summary> /// Convert Byte[] to MemoryStream /// </summary> /// <param name="buffer"></param> /// <returns></returns> private MemoryStream BytesToMemoryStream(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); return ms; }
/// <summary> /// 获取文件md5 /// </summary> /// <param name="filepath"></param> /// <returns></returns> private string GetFileStreamMd5(string filepath) { var oMd5Hasher = new MD5CryptoServiceProvider(); FileStream fStream = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); byte[] arrbytHashValue = oMd5Hasher.ComputeHash(fStream);//占用fStream之后的赋值失效 //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” string strHashData = BitConverter.ToString(arrbytHashValue); //替换- strHashData = strHashData.Replace("-", ""); string strResult = strHashData; oMd5Hasher.Dispose(); fStream.Close(); return strResult; }
//判断文件是否存在 if (File.Exists(txtFilePath)) { StreamReader strinput = File.OpenText(txtFilePath); string input = strinput.ReadToEnd(); strinput.Close(); if (File.Exists(input.Trim())) { return input; } } return filepath; //设置默认路径 //获取桌面路径 string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string fileDir = string.Format(@"{0}\{1}\{2}", dir, this.currentProject.projectName, this.currentPackage.packageName); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } string saveFilePath = fileDir; foreach (DownloadItemControl item in items) { item.tenderDocumentDownloadItemInfo.saveFilePath = saveFilePath + "\" + item.tenderDocumentDownloadItemInfo.displayFileName; item.BeginDownLoad(); //创建文本文档 //重新拼接日志目录 string txtFilePath = item.tenderDocumentDownloadItemInfo.txtfilePath; //创建文本文档路径或打开 FileStream fileStream = new FileStream(txtFilePath, FileMode.Create); //保存路径到文本里 StreamWriter sw = new StreamWriter(fileStream); sw.Write(item.tenderDocumentDownloadItemInfo.saveFilePath); sw.Close(); }
//获取程序目录下模版 private void BtnChooseTenderingTemplate_Click(object sender, RoutedEventArgs e) { // 获得中标通知书模板内容 string filePath = System.AppDomain.CurrentDomain.BaseDirectory + "/DocxTemplate/XXX.docx"; using (FileStream inputStream = new FileStream(filePath, FileMode.Open)) { } }
/// <summary> /// 无损压缩缩放图片 /// </summary> /// <param name="sFile"></param> /// <param name="destWidth"></param> /// <param name="destHeight"></param> /// <returns></returns> public static Stream GetThumbnail(Stream sFile, int destWidth, int destHeight) { Image imgSource = Image.FromStream(sFile); ImageFormat tFormat = imgSource.RawFormat; int sW = 0, sH = 0; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height; 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.White); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; Stream resultStream = new MemoryStream(); try { outBmp.Save(resultStream, tFormat); //ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); //ImageCodecInfo jpegICIinfo = null; //for (int x = 0; x < arrayICI.Length; x++) //{ // if (arrayICI[x].FormatDescription.Equals("JPEG")) // { // jpegICIinfo = arrayICI[x]; // break; // } //} //if (jpegICIinfo != null) //{ // outBmp.Save(resultStream, jpegICIinfo, encoderParams);//dFile是压缩后的新路径 //} //else //{ // outBmp.Save(resultStream, tFormat); //} return resultStream; } catch { return resultStream; } finally { imgSource.Dispose(); outBmp.Dispose(); } }