关于缩放图像的两段代码

时间:2009-11-04 07:56:51

标签: asp.net

我想在今天缩放图片并找到以下示例:

示例#1 http://www.codeproject.com/KB/GDI-plus/imageresize.aspx

经过多次测试后,我编写了以下代码:

static System.Drawing.Image Scale(System.Drawing.Image imgPhoto, int v_iPercent)
{
    int destWidth = (int)(imgPhoto.Width * v_iPercent / 100.0);
    int destHeight = (int)(imgPhoto.Height * v_iPercent / 100.0);
    Bitmap bmPhoto = new Bitmap(imgPhoto, destWidth, destHeight);
    return bmPhoto;
}

我想知道为什么我发现的例子需要使用Graphics.DrawImage函数来缩放图像。

1 个答案:

答案 0 :(得分:1)

您是否看到了Bitmap constructor正在使用的内容:

public Bitmap(Image original, int width, int height) : this(width, height)
{
    using (Graphics graphics = null)
    {
        graphics = Graphics.FromImage(this);
        graphics.Clear(Color.Transparent);
        graphics.DrawImage(original, 0, 0, width, height);
    }
}

它会调用DrawImage