为什么即使将Bitmap设置为Graphics.Clear(Color.Transparent),也会在调整大小时在图像周围显示黑色背景

为什么即使将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.