MVC3 WebImage助手:调整大小是将透明背景转换为黑色

时间:2010-11-15 04:17:17

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-helpers

我正在尝试使用MVC3的WebImage助手创建缩略图。

原始图像是带有透明背景的.png。当我尝试使用以下内容调整大小时:

var image = blob.DownloadByteArray();     

new WebImage(image)
    .Resize(50, 50)
    .Write();

生成的缩略图将原始透明背景替换为黑色背景。

3 个答案:

答案 0 :(得分:6)

上面这个答案很棒,但我做了一些微调并实现了图像的“保持比例”,这样我们就不会得到拉伸的图像了。

    using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Helpers;

public static class ResizePng
{
    private static IDictionary<string, ImageFormat> _transparencyFormats = new Dictionary<string, ImageFormat>(StringComparer.OrdinalIgnoreCase) { { "png", ImageFormat.Png }, { "gif", ImageFormat.Gif } };

    public static WebImage ResizePreserveTransparency(this WebImage image, int width, int height)
    {
        ImageFormat format = null;
        if (!_transparencyFormats.TryGetValue(image.ImageFormat, out format))
        {
            return image.Resize(width, height);
        }

        //keep ratio *************************************
        double ratio = (double)image.Width / image.Height;
        double desiredRatio = (double)width / height;
        if (ratio > desiredRatio)
        {
            height = Convert.ToInt32(width / ratio);
        }
        if (ratio < desiredRatio)
        {
            width = Convert.ToInt32(height * ratio);
        }
        //************************************************

        using (Image resizedImage = new Bitmap(width, height))
        {
            using (Bitmap source = new Bitmap(new MemoryStream(image.GetBytes())))
            {
                using (Graphics g = Graphics.FromImage(resizedImage))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(source, 0, 0, width, height);
                }
            }
            using (MemoryStream ms = new MemoryStream())
            {
                resizedImage.Save(ms, format);
                return new WebImage(ms.ToArray());
            }
        }
    }

}

答案 1 :(得分:2)

您应该为gif和png图像编写自己的resize方法。我已经为Web Image创建了扩展图像的扩展。请参阅下面的方法代码:

public static class WebImageExtension
{
    private static readonly IDictionary<string, ImageFormat> TransparencyFormats =
        new Dictionary<string, ImageFormat>(StringComparer.OrdinalIgnoreCase)
            {{"png", ImageFormat.Png}, {"gif", ImageFormat.Gif}};

    public static WebImage Resize(this WebImage image, int width)
    {
        double aspectRatio = (double)image.Width / image.Height;
        var height = Convert.ToInt32(width/aspectRatio);

        ImageFormat format;

        if (!TransparencyFormats.TryGetValue(image.ImageFormat.ToLower(), out format))
        {
            return image.Resize(width, height);
        }

        using (Image resizedImage = new Bitmap(width, height))
        {
            using (var source = new Bitmap(new MemoryStream(image.GetBytes())))
            {
                using (Graphics g = Graphics.FromImage(resizedImage))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(source, 0, 0, width, height);
                }
            }

            using (var ms = new MemoryStream())
            {
                resizedImage.Save(ms, format);
                return new WebImage(ms.ToArray());
            }
        }
    }
}

答案 2 :(得分:1)

您应该更改.Write以传递预期的输出类型。它使用此传递类型来确定要使用的图像类型。

var image = blob.DownloadByteArray();     

new WebImage(image)
    .Resize(50, 50)
    .Write("png");