/* * * /// <summary> * /// 格式化图片 * /// </summary> * /// <param name="originalImagePath">源图物理路径</param> * /// <param name="formatImagePath">新图物理路径</param> * /// <param name="formatWidth">格式化宽度</param> * /// <param name="formatHeight">格式化高度</param> * /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> * /// <param name="picFormat">图片输出格式</param> * public static void FormatPicture(string originalPath, string formatPath, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) * { * System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalPath); * * int towidth = formatWidth; * int toheight = formatHeight; * * int x = 0; * int y = 0; * int ow = originalImage.Width; * int oh = originalImage.Height; * * if (cut) * { * //指定高宽裁减(不变形) * if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) * { * oh = originalImage.Height; * ow = originalImage.Height * towidth / toheight; * y = 0; * x = (originalImage.Width - ow) / 2; * } * else * { * ow = originalImage.Width; * oh = originalImage.Width * formatHeight / towidth; * x = 0; * y = (originalImage.Height - oh) / 2; * } * } * else * { * //默认指定高度宽度,失真缩放 * * //指定宽度缩放 * if (formatHeight <= 0) * { * formatWidth = originalImage.Width * formatHeight / originalImage.Height; * } * * //指定高度缩放 * else if (formatWidth <= 0) * { * formatHeight = originalImage.Height * formatWidth / originalImage.Width; * } * } * * //新建一个bmp图片 * System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight); * //新建一个画板 * System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image); * //设置高质量插值法 * graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; * //设置高质量,低速度呈现平滑程度 * graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; * //清空画布并以透明背景色填充 * graphics.Clear(System.Drawing.Color.Transparent); * //在指定位置并且按指定大小绘制原图片的指定部分 * graphics.DrawImage( * originalImage, * new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight), * new System.Drawing.Rectangle(x, y, ow, oh), * System.Drawing.GraphicsUnit.Pixel * ); * originalImage.Dispose(); * * System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(picFormat); * * //以指定格式保存图片 * try * { * image.Save(formatPath, imageFormat); * } * catch (Exception ex) * { * throw ex; * } * finally * { * //originalImage.Dispose(); * image.Dispose(); * graphics.Dispose(); * } * } * */ /// <summary> /// 格式化图片 /// </summary> /// <param name="originalImagePath">源图物理路径</param> /// <param name="formatImagePath">新图物理路径</param> /// <param name="formatWidth">格式化宽度</param> /// <param name="formatHeight">格式化高度</param> /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param> /// <param name="picFormat">图片输出格式</param> public static void FormatPicture(string originalPath, string formatPath, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat) { Byte[] fileBinary = File.ReadAllBytes(originalPath); Byte[] formatBinary = FormatPicture(fileBinary, formatWidth, formatHeight, cut, picFormat); if (formatBinary != null) { FileConvert.SaveFile(formatPath, formatBinary); } }
/// <summary> /// 打包压缩文件并返回二进制 /// </summary> /// <param name="kvList"></param> /// <param name="tempFolder"></param> /// <param name="path"></param> /// <returns></returns> public byte[] ZipFileBinarys(IList <KVPair> fileList, string tempFolder) { string sourceDirectoryName = tempFolder + Guid.NewGuid().ToString(); string destinationArchiveFileName = sourceDirectoryName + ".zip"; if (!Directory.Exists(sourceDirectoryName)) { Directory.CreateDirectory(sourceDirectoryName); } foreach (KVPair kv in fileList) { if (kv.Value == null) { continue; } byte[] byteArray; if (kv.Value.GetType().Equals(typeof(byte[]))) { byteArray = (byte[])kv.Value; } else { byteArray = System.Text.Encoding.UTF8.GetBytes(kv.Value.ToString()); } FileConvert.SaveFile(sourceDirectoryName + "/" + kv.Key, byteArray); } ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName); FileStream stream = new FileStream(destinationArchiveFileName, FileMode.Open); byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); stream.Close(); return(buffer); }