标签:文件名 code 生成 bit returns graphic bitmap new type
///
/// 按质量比和尺寸收缩呗数,生成缩略图,保存为另外路劲
///
/// 原始图片文件
/// 质量压缩比
/// 收缩倍数
/// 输出文件名
/// 成功返回true,失败则返回false
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;
}
/**/
///
/// 按质量比和尺寸收缩呗数,生成缩略图;保存在同一路径
///
/// 原始图片文件
/// 质量压缩比
/// 收缩倍数
/// 输出文件名
/// 成功返回true,失败则返回false
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;
}
///
/// 按图片尺寸大小压缩图片 ,保存在不同路径
///
/// 原始图片文件
/// 收缩倍数
/// 成功返回true,失败则返回false
public static bool ImageByMultiple(String sourceFile, int multiple, String outputFile)
{
bool flag = false;
try
{
flag = ThumImageByMultiple(sourceFile, multiple, outputFile);
}
catch
{
flag = false;
}
return flag;
}
///
/// 按图片尺寸大小压缩图片 ,保存在同一路径
///
/// 收缩倍数
/// 成功返回true,失败则返回false
public static bool ImageByMultiple(String sourceFile, int multiple)
{
bool flag = false;
try
{
flag = ThumImageByMultiple(sourceFile, multiple, sourceFile);
}
catch
{
flag = false;
}
return flag;
}
///
/// 质量压缩比,保存在不同一路径
///
/// 原始图片文件
/// 质量压缩比
/// 成功返回true,失败则返回false
public static bool ImageByQuality(String sourceFile, int quality, String outputFile)
{
bool flag = false;
try
{
flag = ThumImageByQuality(sourceFile, quality, outputFile);
}
catch
{
flag = false;
}
return flag;
}
///
/// 按质量压缩比压缩图片,保存在同一路径
///
/// 原始图片文件
/// 质量压缩比
/// 成功返回true,失败则返回false
public static bool ImageByQuality(String sourceFile, int quality)
{
bool flag = false;
try
{
flag = ThumImageByQuality(sourceFile, quality, sourceFile);
}
catch
{
flag = false;
}
return flag;
}
#endregion
#region 调用图片处理具体方法
///
/// 按照图片质量生成图片,
///
/// 原始图片文件
/// 质量压缩比
/// 输出文件名
/// 成功返回true,失败则返回false
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;
}
///
/// 生成缩略图
///
/// 原始图片文件
/// 质量压缩比
/// 收缩倍数
/// 输出文件名
/// 成功返回true,失败则返回false
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;
}
///
/// 按图片尺寸大小压缩图片
///
/// 原始图片文件
/// 收缩倍数
/// 输出文件名
/// 成功返回true,失败则返回false
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;
}
}
///
/// 按图片尺寸大小压缩图片
///
/// 原始图片文件
/// 收缩倍数
/// 输出文件名
/// 成功返回true,失败则返回false
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
///
/// 获取图片编码信息
///
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
C# 图片压缩
标签:文件名 code 生成 bit returns graphic bitmap new type
原文地址:https://www.cnblogs.com/Jayesslee/p/9335507.html