- 积分
- 40165
- 好友
- 记录
- 主题
- 帖子
- 听众
- 收听
|
发表于 2013-3-18 17:54:32
|
显示全部楼层
_天微亮゜。 发表于 2013-3-18 15:58
尺寸要不变的,只是占用字节减小
这个你把我写的固定的修改为你图片的长高就行了,
[code=csharp] /// <summary>
/// 压缩指定尺寸,如果写的和图片大家一样表示大小不变,只是把图片压缩下一些
/// </summary>
/// <param name="oldfile">原文件</param>
/// <param name="newfile">新文件</param>
/// <param name="width">长</param>
/// <param name="height">高</param>
public bool Compress(string oldfile, string newfile,int width,int height)
{
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
Size newSize = new Size(width, height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0; x < arrayICI.Length; x++)
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x]; //设置JPEG编码
break;
}
img.Dispose();
if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
outBmp.Dispose();
return true;
}
catch
{
return false;
}
}[/code] |
|