/// <summary> /// 读取XPS缩略图 /// </summary> /// <param name="res_key"></param> /// <param name="rft"></param> /// <returns></returns> public static string XpsImage(string resFile, ResFileType rft) { string url = ""; try { if (!string.IsNullOrEmpty(resFile)) { string filePath = RWUtility.GetResPath(resFile); if (System.IO.File.Exists(filePath)) { string path = resFile.Split('/')[0] + "/thumbnail.jpg"; if (IsFileExists(path)) { url = GetResUrl(path); } } } if (url.Length == 0) { if (ResConfig.Current.Icons.ContainsKey(rft)) { return(ResConfig.Current.ResUrlDefault + ResConfig.Current.Icons[rft].Default); } } } catch (Exception ex) { OSeage.Common.Log.Logger.Error(string.Format("[{0}]XpsImage Error:{1}", resFile, ex.ToString())); } return(url); }
/// <summary> /// 删除文件及文件夹 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool DeleteToFiles(string filePath) { bool flag = false; string path = RWUtility.GetResPath(Path.GetFileName(Path.GetDirectoryName(filePath))); if (Directory.Exists(path)) { try { Directory.Delete(path, true); flag = true; } catch { } } return(flag); }
/// <summary> /// 上传附件 /// </summary> /// <param name="file">含文件内容的HttpPostedFileBase</param> /// <param name="savePath">相对路径</param> /// <returns>相对路径(不含ResourceDir) + 文件名</returns> public static string CopyImageToResFiles(string file) { DateTime time = DateTime.Now; string filePath = time.ToString("yyyyMMddHHmmss") + time.Millisecond.ToString("000") + "/"; string path = RWUtility.GetResPath(filePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = Path.GetFileName(file.Replace(" ", "")); Bitmap map = new Bitmap(file); map.Save(path + fileName); return(filePath + fileName); }
/// <summary> /// 图片缩放 /// </summary> /// <param name="filePath"></param> /// <param name="rft"></param> /// <returns></returns> public static string PicZoomAuto(string filePath, ResFileType rft) { filePath = RWUtility.GetResPath(filePath); string savePath = string.Empty; if (ResConfig.Current.Icons.ContainsKey(rft)) { IconInfo size = ResConfig.Current.Icons[rft]; string dirPath = Path.GetDirectoryName(filePath) + string.Format("\\{0}x{1}\\", size.Width, size.Height); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } ImgUtil.ZoomAuto(filePath, dirPath + Path.GetFileName(filePath), size.Width, size.Height, null, null, 100); savePath = dirPath + Path.GetFileName(filePath); } return(savePath); }
/// <summary> /// 上传附件 /// </summary> /// <param name="file">含文件内容的HttpPostedFileBase</param> /// <param name="lengthName">是否以文件大小作为文件名</param> /// <returns>返回yyyyMMddHHmmssSSS+fileName</returns> public static string UploadToServer(HttpPostedFileBase file, bool lengthName) { if (file.ContentLength > 0) { DateTime time = DateTime.Now; string filePath = time.ToString("yyyyMMddHHmmss") + time.Millisecond.ToString("000") + "/"; string path = RWUtility.GetResPath(filePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = Path.GetFileName(file.FileName.Replace(" ", "")); if (lengthName) { fileName = file.ContentLength + Path.GetExtension(file.FileName); } file.SaveAs(path + fileName); return(filePath + fileName); } else { return(string.Empty); } }
/// <summary> /// 必须要有Content-Length和Content-Disposition以及ContentType /// </summary> /// <param name="filePath"></param> /// <param name="contentType"></param> /// <param name="Response"></param> public static void DownLoad(string filePath, string fileName, string contentType, HttpResponseBase Response, bool deleteAfterDownload = false) { //指定块大小 long chunkSize = 204800; //建立一个200K的缓冲区 byte[] buffer = new byte[chunkSize]; //已读的字节数 long dataToRead = 0; FileStream stream = null; try { //打开文件 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http头 Response.ContentType = contentType; //解决,火狐浏览器下载时,中文名乱码 if (HttpContext.Current.Request.Browser.Browser == "Firefox") { //Response.AddHeader("Content-Disposition", "attachement;filename*=utf-8'zh_cn'" + fileName); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); } else { Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName).Replace("+", "%20")); } Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); Response.Clear(); dataToRead -= length; } else { //防止client失去连接 dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); if (deleteAfterDownload && File.Exists(filePath)) { string path = Path.GetDirectoryName(filePath); if (path.Length > RWUtility.GetResPath("").Length) { try { Directory.Delete(Path.GetDirectoryName(filePath), true); } catch { } } } } Response.Close(); } }
/// <summary> /// 创建缩略图 /// </summary> public static string CutAvatar(string imgSrc, int x, int y, int width, int height, long Quality, ResFileType rft, int t) { try { imgSrc = RWUtility.GetResPath(imgSrc); string dirPath = string.Empty; if (ResConfig.Current.Icons.ContainsKey(rft)) { IconInfo size = ResConfig.Current.Icons[rft]; dirPath = Path.GetDirectoryName(imgSrc) + string.Format("\\{0}x{1}\\", size.Width, size.Height); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } Image original = Image.FromFile(imgSrc); Bitmap img = new Bitmap(t, t, PixelFormat.Format24bppRgb); img.MakeTransparent(img.GetPixel(0, 0)); img.SetResolution(72, 72); using (Graphics gr = Graphics.FromImage(img)) { if (original.RawFormat.Equals(ImageFormat.Jpeg) || original.RawFormat.Equals(ImageFormat.Png) || original.RawFormat.Equals(ImageFormat.Bmp)) { gr.Clear(Color.Transparent); } if (original.RawFormat.Equals(ImageFormat.Gif)) { gr.Clear(Color.White); } gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.SmoothingMode = SmoothingMode.AntiAlias; gr.CompositingQuality = CompositingQuality.HighQuality; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; gr.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; using (var attribute = new System.Drawing.Imaging.ImageAttributes()) { attribute.SetWrapMode(WrapMode.TileFlipXY); gr.DrawImage(original, new Rectangle(0, 0, t, t), x, y, width, height, GraphicsUnit.Pixel, attribute); } } ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); if (original.RawFormat.Equals(ImageFormat.Jpeg)) { myImageCodecInfo = GetEncoderInfo("image/jpeg"); } else if (original.RawFormat.Equals(ImageFormat.Png)) { myImageCodecInfo = GetEncoderInfo("image/png"); } else if (original.RawFormat.Equals(ImageFormat.Gif)) { myImageCodecInfo = GetEncoderInfo("image/gif"); } else if (original.RawFormat.Equals(ImageFormat.Bmp)) { myImageCodecInfo = GetEncoderInfo("image/bmp"); } System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, Quality); myEncoderParameters.Param[0] = myEncoderParameter; img.Save(dirPath + Path.GetFileName(imgSrc), myImageCodecInfo, myEncoderParameters); } return(dirPath + Path.GetFileName(imgSrc)); } catch { return(""); } }