标签:文件名 code 生成 bit returns graphic bitmap new type
/// <summary> /// 按质量比和尺寸收缩呗数,生成缩略图,保存为另外路劲 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> public static bool getThumImage(String sourceFile, long quality, int multiple, String outputFile) { bool flag = false; try { flag = ThumImage(sourceFile, quality, multiple, outputFile); } catch { flag = false; } return flag; } /**/ /// <summary> /// 按质量比和尺寸收缩呗数,生成缩略图;保存在同一路径 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> public static bool getThumImage(String sourceFile, long quality, int multiple) { bool flag = false; try { flag = ThumImage(sourceFile, quality, multiple, sourceFile); } catch { flag = false; } return flag; } /// <summary> /// 按图片尺寸大小压缩图片 ,保存在不同路径 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="multiple">收缩倍数</param> /// <returns>成功返回true,失败则返回false</returns> public static bool ImageByMultiple(String sourceFile, int multiple, String outputFile) { bool flag = false; try { flag = ThumImageByMultiple(sourceFile, multiple, outputFile); } catch { flag = false; } return flag; } /// <summary> /// 按图片尺寸大小压缩图片 ,保存在同一路径 /// </summary> /// <param name="multiple">收缩倍数</param> /// <returns>成功返回true,失败则返回false</returns> public static bool ImageByMultiple(String sourceFile, int multiple) { bool flag = false; try { flag = ThumImageByMultiple(sourceFile, multiple, sourceFile); } catch { flag = false; } return flag; } /// <summary> /// 质量压缩比,保存在不同一路径 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <returns>成功返回true,失败则返回false</returns> public static bool ImageByQuality(String sourceFile, int quality, String outputFile) { bool flag = false; try { flag = ThumImageByQuality(sourceFile, quality, outputFile); } catch { flag = false; } return flag; } /// <summary> /// 按质量压缩比压缩图片,保存在同一路径 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <returns>成功返回true,失败则返回false</returns> public static bool ImageByQuality(String sourceFile, int quality) { bool flag = false; try { flag = ThumImageByQuality(sourceFile, quality, sourceFile); } catch { flag = false; } return flag; } #endregion #region 调用图片处理具体方法 /// <summary> /// 按照图片质量生成图片, /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> private static bool ThumImageByQuality(String sourceFile, long quality, String outputFile) { bool flag = false; try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth, yWidth); sourceImage.Dispose(); g.Dispose(); newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); flag = true; } catch { flag = false; } return flag; } /// <summary> /// 生成缩略图 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="quality">质量压缩比</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> private static bool ThumImage(String sourceFile, long quality, int multiple, String outputFile) { bool flag = false; try { long imageQuality = quality; Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; EncoderParameters myEncoderParameters = new EncoderParameters(1); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, imageQuality); myEncoderParameters.Param[0] = myEncoderParameter; float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); sourceImage.Dispose(); g.Dispose(); newImage.Save(outputFile, myImageCodecInfo, myEncoderParameters); flag = true; } catch { flag = false; } return flag; } /// <summary> /// 按图片尺寸大小压缩图片 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> public static bool ThumImageByMultiple(String sourceFile, int multiple, String outputFile) { try { Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); float xWidth = sourceImage.Width; float yWidth = sourceImage.Height; Bitmap newImage = new Bitmap((int)(xWidth / multiple), (int)(yWidth / multiple)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth / multiple, yWidth / multiple); sourceImage.Dispose(); g.Dispose(); newImage.Save(outputFile); return true; } catch { return false; } } /// <summary> /// 按图片尺寸大小压缩图片 /// </summary> /// <param name="sourceFile">原始图片文件</param> /// <param name="multiple">收缩倍数</param> /// <param name="outputFile">输出文件名</param> /// <returns>成功返回true,失败则返回false</returns> public static bool FixSize(String sourceFile, int xWidth, int yWidth, String outputFile) { try { Bitmap sourceImage = new Bitmap(sourceFile); ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg"); Bitmap newImage = new Bitmap((int)(xWidth), (int)(yWidth)); Graphics g = Graphics.FromImage(newImage); g.DrawImage(sourceImage, 0, 0, xWidth, yWidth); sourceImage.Dispose(); g.Dispose(); newImage.Save(outputFile); return true; } catch { return false; } } #endregion getThumImage /// <summary> /// 获取图片编码信息 /// </summary> private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for (j = 0; j < encoders.Length; ++j) { if (encoders[j].MimeType == mimeType) return encoders[j]; } return null; }
标签:文件名 code 生成 bit returns graphic bitmap new type
原文地址:https://www.cnblogs.com/Jayesslee/p/9335507.html