MVC3 WebImage helper: resize is converting transparent background to black

空扰寡人 提交于 2019-12-04 11:59:55

问题


I'm trying to create a thumbnail using MVC3's WebImage helper.

The original image is a .png with a transparent background. When I try and resize it with the following:

var image = blob.DownloadByteArray();     

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

The resulting thumbnail replaces the original transparent background with a black background.


回答1:


This above answer is great but i did some fine-tuning and implemented the "keep ratio" of the image so that we don't end up with stretched images.

    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());
            }
        }
    }

}




回答2:


You should write you own resize method for gif and png images. I have created extension for Web Image for resizing images. See the code of my method below:

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());
            }
        }
    }
}



回答3:


You should alter the .Write to pass your expected output type. It uses this passed type to determine what type of image to use.

var image = blob.DownloadByteArray();     

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


来源:https://stackoverflow.com/questions/4181347/mvc3-webimage-helper-resize-is-converting-transparent-background-to-black

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!