为什么即使将Bitmap设置为Graphics.Clear(Color.Transparent),也会在调整大小时在图像周围显示黑色背景 [英] Why am I getting a black background around my images when resizing even when Bitmap is set to Graphics.Clear(Color.Transparent)

查看:2018
本文介绍了为什么即使将Bitmap设置为Graphics.Clear(Color.Transparent),也会在调整大小时在图像周围显示黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个图像上传工具,可以调整图像大小以适应固定大小,但它会在图像周围的填充空间中添加黑色背景而不是透明背景。

I'm building an Image upload tool that resizes an image to fit a fixed size but it's adding a black background instead of a transparent one to the filler space around the image.

我读过Bitmap需要设置为带有Alpha层的PixelFormat,我可以将Graphics clear颜色设置为透明但我仍然遇到同样的问题。

I've read that the Bitmap needs to be set to a PixelFormat with an Alpha layer and that I can set the Graphics clear colour to transparent but I'm still getting the same problem.

我的图片主要是jpeg。下面是代码:

My images are mostly jpegs. Here is the code:

private void ResizeImage(Image Original, Int32 newWidth, Int32 newHeight, String pathToSave)
    {
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;

        int originalWidth = Original.Width;
        int originalHeight = Original.Height;
        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)newWidth / (float)originalWidth);
        nPercentH = ((float)newHeight / (float)originalHeight);

        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((newWidth -
                          (originalWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((newHeight -
                          (originalHeight * nPercent)) / 2);
        }

        int destWidth = (int)(originalWidth * nPercent);
        int destHeight = (int)(originalHeight * nPercent);


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

            bmp.SetResolution(Original.HorizontalResolution, Original.VerticalResolution);
            using (Graphics Graphic = Graphics.FromImage(bmp))
            {
                Graphic.CompositingQuality = CompositingQuality.HighQuality;
                Graphic.Clear(Color.Red);
                Graphic.CompositingMode = CompositingMode.SourceCopy;
                Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                Graphic.DrawImage(
                    Original,
                    new Rectangle(destX, destY, destWidth, destHeight),
                    new Rectangle(sourceX, sourceY, originalWidth, originalHeight),
                    GraphicsUnit.Pixel
                    );

                bmp.Save(pathToSave,Original.RawFormat);

            }

}


推荐答案

          Graphic.Clear(Color.Red);

不,你把背景变成红色,而不是黑色。如果要将背景的alpha设置为0,请使用Color.Transparent。或者只省略Clear(),它是新位图的默认值。并且在Save()调用中避免使用Original.RawFormat,您不希望使用不支持透明度的图像格式。 Png总是很好。并且确保用于显示结果位图的任何方法都支持透明度。具有明确定义的背景颜色。如果没有,你会变黑,Color.Transparent的R,G和B为0.黑色。

No, you made that background red, not black. Use Color.Transparent if you want to have the alpha of the background set to 0. Or just omit the Clear(), it is the default for a new bitmap. And avoid Original.RawFormat in the Save() call, you don't want to use an image format that doesn't support transparency. Png is always good. And be sure that whatever method you use to display the resulting bitmap supports transparency as well. With a well defined background color. You'll get black when it doesn't, Color.Transparent has R, G and B at 0. Black.

这篇关于为什么即使将Bitmap设置为Graphics.Clear(Color.Transparent),也会在调整大小时在图像周围显示黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆