Asp.net MVC3 WebImage助手:调整大小正在将透明背景转换为黑色

Asp.net MVC3 WebImage助手:调整大小正在将透明背景转换为黑色,asp.net,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-helpers,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(); 生成的缩略图将原始透明背景替换为黑色背景。您应该更改。Write以传递预期的输出类型。它使用此传递的类型来确定要使用的图像类型 var image = blob.Download

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

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

var image = blob.DownloadByteArray();     

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

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

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

var image = blob.DownloadByteArray();     

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

您应该为gif和png图像编写自己的调整大小方法。我已经创建了用于调整图像大小的Web图像扩展。请参见下面我的方法的代码:

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());
            }
        }
    }
}
公共静态类WebImageExtension
{
私有静态只读IDictionary透明格式=
新字典(StringComparer.OrdinalIgnoreCase)
{{“png”,ImageFormat.png},{“gif”,ImageFormat.gif};
公共静态WebImage调整大小(此WebImage图像,整数宽度)
{
double aspectRatio=(double)image.Width/image.Height;
变量高度=转换为32(宽度/纵横比);
图像格式;
if(!TransparencyFormats.TryGetValue(image.ImageFormat.ToLower(),out format))
{
返回图像。调整大小(宽度、高度);
}
使用(图像大小图像=新位图(宽度、高度))
{
使用(var source=new位图(new MemoryStream(image.GetBytes()))
{
使用(Graphics g=Graphics.FromImage(resizedImage))
{
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 DrawImage(源、0、0、宽度、高度);
}
}
使用(var ms=new MemoryStream())
{
resizedImage.Save(ms,格式);
返回新的WebImage(ms.ToArray());
}
}
}
}

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

    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());
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用系统图;
使用系统、绘图、成像;
使用System.IO;
使用System.Web.Helpers;
公共静态类ResizePng
{
私有静态IDictionary _transparencyFormats=新字典(StringComparer.OrdinalIgnoreCase){{{“png”,ImageFormat.png},{“gif”,ImageFormat.gif};
公共静态WebImage ResizePreserveTransparency(此WebImage图像,整数宽度,整数高度)
{
ImageFormat=null;
if(!\u transparencyFormats.TryGetValue(image.ImageFormat,out格式))
{
返回图像。调整大小(宽度、高度);
}
//保持率*************************************
双重比率=(双重)image.Width/image.Height;
双期望比率=(双)宽度/高度;
如果(比率>理想比率)
{
高度=转换为32(宽度/比率);
}
if(比率<理想比率)
{
宽度=转换为32(高度*比率);
}
//************************************************
使用(图像大小图像=新位图(宽度、高度))
{
使用(位图源=新位图(新内存流(image.GetBytes()))
{
使用(Graphics g=Graphics.FromImage(resizedImage))
{
g、 SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g、 DrawImage(源、0、0、宽度、高度);
}
}
使用(MemoryStream ms=new MemoryStream())
{
resizedImage.Save(ms,格式);
返回新的WebImage(ms.ToArray());
}
}
}

}

我在WebImage.GetBytes(“png”)中看到了这一点。