【问题标题】:scaling an image, but keep its proportions缩放图像,但保持其比例
【发布时间】:2010-12-31 02:23:14
【问题描述】:

我想缩放图像,但我不希望图像看起来歪斜。

图片必须为 115x115(长 x 宽)。

图片的高度(长度)不能超过 115 像素,但如果需要,宽度可以小于 115,但不能超过。

这很棘手吗?

【问题讨论】:

标签: c# asp.net image-processing image-scaling


【解决方案1】:

您希望缩放图像并保持纵横比

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;

应该对您有所帮助,如果您对此想法感兴趣:Wikpedia article on Image Aspect Ratio

【讨论】:

  • 刚刚尝试过这段代码(在搜索相同的基本问题时在其他地方找到了它),如果你从一个宽度和高度都为大于最大值 - 您需要根据原始图像的最大尺寸应用第一次或第二次缩放,否则您最终会得到一个尺寸大于任一缩放中允许的最大值。
【解决方案2】:

根据 Brij 的回答,我做了这个扩展方法:

/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxWidth">Max width</param>
/// <param name="maxHeight">Max height</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, int maxWidth, int maxHeight)
{
    double scale = 1;

    if (img.Width > maxWidth || img.Height > maxHeight)
    {
        double scaleW, scaleH;

        scaleW = maxWidth / (double)img.Width;
        scaleH = maxHeight / (double)img.Height;

        scale = scaleW < scaleH ? scaleW : scaleH;
    }

    return img.Resize((int)(img.Width * scale), (int)(img.Height * scale));
}

/// <summary>
/// Resize image to max dimensions
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="maxDimensions">Max image size</param>
/// <returns>Scaled image</returns>
public static Image Scale(this Image img, Size maxDimensions)
{
    return img.Scale(maxDimensions.Width, maxDimensions.Height);
}

调整大小方法:

/// <summary>
/// Resize the image to the given Size
/// </summary>
/// <param name="img">Current Image</param>
/// <param name="width">Width size</param>
/// <param name="height">Height size</param>
/// <returns>Resized Image</returns>
public static Image Resize(this Image img, int width, int height)
{
    return img.GetThumbnailImage(width, height, null, IntPtr.Zero);
}

【讨论】:

  • 而不是 img.Height,img.Source.Height 为我工作(VS 2010 .net 4)。另外,Source 用于 Width
【解决方案3】:

你需要保持纵横比:

float scale = 0.0;

    if (newWidth > maxWidth || newHeight > maxHeight)
    {
        if (maxWidth/newWidth < maxHeight/newHeight)
        {
            scale = maxWidth/newWidth;
        }
        else
        {
            scale = maxHeight/newHeight;
        }
        newWidth = newWidth*scale;
        newHeight = newHeight*scale;

    }

在代码中,最初 newWidth/newHeight 是图像的宽度/高度。

【讨论】:

  • 如果 newHeight 或 newWidth 是整数,它将无法正常工作,您需要转换为 float
【解决方案4】:

看看Bertrands blog post关于使用 GDI 和 WPF 缩放图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    相关资源
    最近更新 更多