/// <summary>
/// 降低图片分辨率,按比例(1~100),比例失效 则按自定义长宽
/// </summary>
/// <param name="image"></param>
/// <param name="ratePic"></param>
/// <param name="widthPic"></param>
/// <param name="heightPic"></param>
/// <returns></returns>
public static Image LowPicPoint(Image image, int ratePic, int widthPic, int heightPic)
{
Graphics grap = null;
try
{
int width, height;
if (1 <= ratePic && ratePic <= 100)
{
width = Convert.ToInt32(image.Width * (Convert.ToDouble(ratePic) / 100));
height = Convert.ToInt32(image.Height * (Convert.ToDouble(ratePic) / 100));
}
else
{
width = widthPic;
height = heightPic;
}
Image newImage = new Bitmap(width, height);
grap = Graphics.FromImage(newImage);
grap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grap.DrawImage(image, new Rectangle(0, 0, width, height));
return (newImage);
}
catch (Exception ex)
{
throw new Exception("降低图片分辨率失效!", ex);
}
finally
{
if (grap != null) grap.Dispose();
}
}
/// 无损压缩图片
/// <param name="sFile">原图片</param>
/// <param name="dFile">压缩后保存位置</param>
/// <param name="dHeight">高度</param>
/// <param name="dWidth"></param>
/// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>
/// <returns></returns>
public static bool GetPicThumbnail(string sFile, string dFile, int flag, int dWidth, int dHeight)
{
System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile);
ImageFormat tFormat = iSource.RawFormat;
int sW = 0, sH = 0;
//按比例缩放
Size tem_size = new Size(iSource.Width, iSource.Height);
if (tem_size.Width > dHeight || tem_size.Width > dWidth)
{
if ((tem_size.Width * dHeight) > (tem_size.Width * dWidth))
{
sW = dWidth;
sH = (dWidth * tem_size.Height) / tem_size.Width;
}
else
{
sH = dHeight;
sW = (tem_size.Width * dHeight) / tem_size.Height;
}
}
else
{
sW = tem_size.Width;
sH = tem_size.Height;
}
Bitmap ob = new Bitmap(dWidth, dHeight);
Graphics g = Graphics.FromImage(ob);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle(0, 0, dWidth, dHeight), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
//以下代码为保存图片时,设置压缩质量
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
try
{
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)
{
iSource.Dispose();
FileUtils.Delete(sFile);
ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
iSource.Dispose();
FileUtils.Delete(sFile);
ob.Save(dFile, tFormat);
}
return true;
}
catch
{
return false;
}
finally
{
iSource.Dispose();
ob.Dispose();
}
}
C#图片压缩
原创
©著作权归作者所有:来自51CTO博客作者朋克版小红帽的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# 无损压缩图片
/// 无损压缩图片 /// <par
System Image 压缩率 -
C#压缩
C#压缩
C#压缩 -
简单的C#图片压缩类-winform
简单的C#图片压缩类-winform,学习中。。。。。。。
职场 休闲 winform 压缩 C#图片 -
c#压缩实现
代码 /// <summary> /// table XML序列化并压缩 /// </summary> &n
加密 c# xml table byte -
C# zip压缩zip压缩