【问题标题】:resize image in asp.net在 asp.net 中调整图像大小
【发布时间】:2011-07-02 09:14:52
【问题描述】:

我有这段代码可以调整图像大小,但图像看起来不太好:

public Bitmap ProportionallyResizeBitmap(Bitmap src, int maxWidth, int maxHeight)
{

    // original dimensions
    int w = src.Width;
    int h = src.Height;
    // Longest and shortest dimension
    int longestDimension = (w > h) ? w : h;
    int shortestDimension = (w < h) ? w : h;
    // propotionality
    float factor = ((float)longestDimension) / shortestDimension;
    // default width is greater than height
    double newWidth = maxWidth;
    double newHeight = maxWidth / factor;
    // if height greater than width recalculate
    if (w < h)
    {
        newWidth = maxHeight / factor;
        newHeight = maxHeight;
    }
    // Create new Bitmap at new dimensions
    Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
    using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
        g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
    return result;
}

【问题讨论】:

标签: c# asp.net


【解决方案1】:

尝试将图形对象的InterpolationMode 设置为某个值,例如HighQualityBicubic。这应该确保调整大小/缩放的图像看起来比“默认”更好。

所以,在您发布的代码中,而不是这样做:

// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
   g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
return result;

尝试这样做:

// Create new Bitmap at new dimensions
Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);
}
return result;

(注意将InterpolationMode 属性设置为InterpolationMode 枚举值之一的行)。

请看这个链接:
How to: Use Interpolation Mode to Control Image Quality During Scaling
有关在调整大小/缩放时控制图像质量的更多信息。

另请参阅此 CodeProject 文章:
Resizing a Photographic image with GDI+ for .NET
有关各种 InterpolationMode 枚举设置将对图像产生的不同视觉效果的信息。 (大约在文章的三分之二处,在标题为“最后一件事......”的部分下)。

【讨论】:

    【解决方案2】:

    如果您需要即时调整大小,我建议您尝试我最近编写的 HttpHandler,I've posted the code on my blog(对不起,但它是意大利语)。

    通过一些修改,您还可以使用代码将转换后的图像保存在磁盘上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 2010-09-20
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多