首页 新闻 会员 周边 捐助

使用Graphics 保存 图片质量很差,怎么回事?

0
[已解决问题] 解决于 2012-03-19 20:43

原本宽度为300 的图片ImagePhoto 大小为 30KB

FixedSize(ImagePhoto,300) 之后 图片 变为 20KB 了,图片质量下降了很多

怎么弄让图片质量和原来的一样呢?

        public static Image FixedSize(Image imgPhoto, int Width)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;

int w, h;
if (sourceWidth > Width)
{
decimal nPercent = (decimal)sourceWidth / Width;
w = Width;
h = (int)Math.Round((decimal)sourceHeight / nPercent, 0);
}
else
{
w = sourceWidth;
h = sourceHeight;
}



Bitmap bmPhoto = new Bitmap(w, h, imgPhoto.PixelFormat);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.CompositingQuality = CompositingQuality.HighQuality;
grPhoto.SmoothingMode = SmoothingMode.HighQuality;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;


grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, w, h),
new Rectangle(0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}
fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2012-03-19 14:36
< >
分享
最佳答案
0

如果是JPG的话,参考以下代码:

var imageCodeInfo = GetEncoderInfo("image/jpeg");
var myEncoder = System.Drawing.Imaging.Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, 99L);
thumbNail.Save(thumbUrl, imageCodeInfo, myEncoderParameters);
dudu | 高人七级 |园豆:29618 | 2012-03-19 20:42

非常感谢,就是这样

fun5 | 园豆:4 (初学一级) | 2012-03-19 20:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册