按比例缩放图片大小

转自:http://dev.mjxy.cn/a-166.aspx

private Image ScaleImage(Image source, int MaxWidth, int MaxHeight)
{
    float MaxRatio = MaxWidth / (float) MaxHeight;
    float ImgRatio = source.Width / (float) source.Height;

    if (source.Width > MaxWidth)
        return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth / ImgRatio, 0)));

    if (source.Height > MaxHeight)
        return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio, 0), MaxHeight));

    return source;
}

posted @ 2011-07-08 15:32  敏捷学院  阅读(148)  评论(0编辑  收藏  举报