Alpha 通道透明度和调整图像文件大小

发布于 2024-09-13 20:19:03 字数 1378 浏览 4 评论 0原文

我正在使用以下代码来调整 tif 的大小。 tif 有一个用于透明度的 Alpha 通道。我正在尝试调整该图像的大小并尊重透明度,但目前它以黑色背景出现。有什么想法吗?

public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height)
        {
            Size NewSize = new Size(Width, Height);

            using (Image OriginalImage = Image.FromFile(OriginalImagePath))
            {
                //Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead.
                PixelFormat Format = OriginalImage.PixelFormat;
                if (Format.ToString().Contains("Indexed"))
                    Format = PixelFormat.Format24bppRgb;

                using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat))
                {
                    using (Graphics Canvas = Graphics.FromImage(NewImage))
                    {
                        Canvas.SmoothingMode = SmoothingMode.AntiAlias;
                        Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize));
                        NewImage.Save(NewImagePath, OriginalImage.RawFormat);
                    }
                }
            }
        }

    }

I'm using the following code to resize a tif. The tif has an alpha channel set for transparency. I'm trying to resize this image and honour the transparency but at the moment it's coming out with a black background. Any ideas?

public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height)
        {
            Size NewSize = new Size(Width, Height);

            using (Image OriginalImage = Image.FromFile(OriginalImagePath))
            {
                //Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead.
                PixelFormat Format = OriginalImage.PixelFormat;
                if (Format.ToString().Contains("Indexed"))
                    Format = PixelFormat.Format24bppRgb;

                using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat))
                {
                    using (Graphics Canvas = Graphics.FromImage(NewImage))
                    {
                        Canvas.SmoothingMode = SmoothingMode.AntiAlias;
                        Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize));
                        NewImage.Save(NewImagePath, OriginalImage.RawFormat);
                    }
                }
            }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶…霜飞 2024-09-20 20:19:03

试试这个:

if (Format.ToString().Contains("Indexed"))
    Format = PixelFormat.Format32bppArgb;

Format32bppArgb 指定像素格式的 Alpha 通道。

我认为你打算这样做:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format))

编辑:

实际上,尝试将 NewImage 上的像素格式强制为 Format32bppArgb像这样:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height,
    PixelFormat.Format32bppArgb))

Try this:

if (Format.ToString().Contains("Indexed"))
    Format = PixelFormat.Format32bppArgb;

Format32bppArgb specifies an alpha channel in the pixel format.

And I think you meant to do this:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format))

EDIT:

Actually, try just forcing the pixel format on the NewImage to Format32bppArgb like so:

using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height,
    PixelFormat.Format32bppArgb))
遮云壑 2024-09-20 20:19:03
Canvas.Clear(Color.Transparent)

在你进行 blit 之前。

Canvas.Clear(Color.Transparent)

before you blit.

如何视而不见 2024-09-20 20:19:03

我实际上发现,由于使用 Photoshop 以 tiff 格式存储透明度的方式,最好通过自动化 Photoshop 然后处理生成的 png 来创建 png。

I actually found out that due to the way the transparency is stored in the tiff format with photoshop it was better to create png's by automating photoshop and then crunching off the resulting png.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文