【问题标题】:Using Graphics.DrawImage , changes the background of my transparent PNG images , to black使用 Graphics.DrawImage ,将我的透明 PNG 图像的背景更改为黑色
【发布时间】:2017-01-14 08:21:00
【问题描述】:

我已经编写了这个方法来调整我的图像大小,但是我的透明 PNG 图像的返回图像具有黑色背景。 解决办法是什么?

我尝试过Bitmap.MakeTransparent()Graphics.Clear(),但无法解决我的问题。 我检查了所有相关问题,但找不到任何有用的答案。

public HttpResponseMessage ImageResizer(string path, int w,int h)
    {

        var imagePath = HttpContext.Current.Server.MapPath(path); 
        Bitmap image;
        try
        {
            image = (Bitmap)System.Drawing.Image.FromFile(imagePath);
        }
        catch
        {
            HttpResponseMessage hrm = new HttpResponseMessage();
            return hrm;
        }

        int originalWidth = image.Width;
        int originalHeight = image.Height;

        // New width and height based on aspect ratio
        int newWidth = w;
        int newHeight = h;

        // Convert other formats (including CMYK) to RGB.
        Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
        // Draws the image in the specified size with quality mode set to HighQuality
        using (Graphics graphics = Graphics.FromImage(newImage))
        {
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (var attribute = new ImageAttributes())
            {
                attribute.SetWrapMode(WrapMode.TileFlipXY);

                // draws the resized image to the bitmap
                graphics.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height), new Rectangle(0, 0, originalWidth, originalHeight), GraphicsUnit.Pixel);
            }
        }
        // Get an ImageCodecInfo object that represents the PNG codec.
        ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Png);

        // Create an Encoder object for the Quality parameter.
        Encoder encoder = Encoder.Quality;

        // Create an EncoderParameters object. 
        EncoderParameters encoderParameters = new EncoderParameters(1);

        // Save the image as a PNG file with quality level.
        EncoderParameter encoderParameter = new EncoderParameter(encoder, 10);
        encoderParameters.Param[0] = encoderParameter;
        var splitPath = imagePath.Split('.');
        string newPath = splitPath[0] + "ss5.png";
        newImage.Save(newPath, ImageFormat.Png);

        MemoryStream memoryStream = new MemoryStream();
        newImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new ByteArrayContent(memoryStream.ToArray());
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
        response.Content.Headers.ContentLength = memoryStream.Length;
        return response;
    }

    private ImageCodecInfo GetEncoderInfo(ImageFormat format)
    {
        return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
    }

【问题讨论】:

    标签: c# asp.net asp.net-web-api


    【解决方案1】:

    您在 Bitmap 构造函数中使用了 PixelFormat.Format24bppRgb。该格式将您的位图限制为 3 个通道:红色、绿色和蓝色。因此,您正在创建的位图不支持 alpha(也称为透明度),并将默认为纯黑色图像。当您绘制具有透明度的图像时,该图像的 alpha 将被预乘或丢弃,具体取决于其格式。

    如果你想用透明保存你的新图像,你需要用PixelFormat.Format32bppArgb声明它:

    Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
    

    【讨论】:

    • 感谢您的解决方案,效果很好。图像仍然从浏览器缓存中加载,我认为您的解决方案不起作用。谢谢。
    猜你喜欢
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2011-02-12
    • 1970-01-01
    • 2015-04-20
    • 2021-10-20
    • 2017-06-06
    相关资源
    最近更新 更多