6

MVC3 の WebImage ヘルパーを使用してサムネイルを作成しようとしています。

元の画像は、背景が透明な .png です。次のようにサイズを変更しようとすると:

var image = blob.DownloadByteArray();     

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

結果のサムネイルでは、元の透明な背景が黒い背景に置き換えられます。

4

3 に答える 3

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

}

于 2013-03-21T09:46:13.163 に答える
2

gif および png 画像の独自のサイズ変更メソッドを作成する必要があります。画像のサイズを変更するための 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());
            }
        }
    }
}
于 2012-04-06T15:51:15.350 に答える
1

.Write予想される出力タイプを渡すように変更する必要があります。この渡されたタイプを使用して、使用するイメージのタイプを決定します。

var image = blob.DownloadByteArray();     

new WebImage(image)
    .Resize(50, 50)
    .Write("png");
于 2010-11-17T17:55:18.890 に答える