如何绘制不透明度为 50% 的位图?

发布于 2024-08-04 06:52:30 字数 218 浏览 1 评论 0原文

我有一个带有 Alpha 通道的 .png 文件,我将其用作面板控件上的背景图像。在某些情况下,该控制被禁用。当它被禁用时,我希望背景图像为 50% 透明,以便用户获得有关控件状态的某种视觉指示。

有谁知道如何使位图图像 50% 透明?

我在这个阶段唯一可能的解决方案是将位图图像绘制到新的位图,然后使用面板的背景颜色在其顶部绘制。虽然这有效,但这不是我首选的解决方案,因此提出了这个问题。

I have a .png file, with alpha channel, that I use as the BackgroundImage on a Panel control. Under certain circumstances the control is disabled. When it is disabled I want the background image to be 50% transparent so that the user gets some sort of visual indication as to the controls state.

Does anyone know how I can make a Bitmap image 50% transparent?

The only possible solution I have at this stage is to draw the bitmap image to a new bitmap and then draw over the top of it using the background colour of the panel. Whilst this works it is not my prefered solution, hence this question.

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

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

发布评论

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

评论(3

鸠魁 2024-08-11 06:52:30

下面是一些向图像添加 Alpha 通道的代码。如果您想要 50% alpha,则可以将 alpha 参数设置为 128。请注意,这会创建位图的副本...

    public static Bitmap AddAlpha(Bitmap currentImage, byte alpha)
    {
        Bitmap alphaImage;
        if (currentImage.PixelFormat != PixelFormat.Format32bppArgb)
        {
            alphaImage = new Bitmap(currentImage.Width, currentImage.Height, PixelFormat.Format32bppArgb);
            using (Graphics gr = Graphics.FromImage(tmpImage))
            {
                gr.DrawImage(currentImage, 0, 0, currentImage.Width, currentImage.Height);
            }
        }
        else
        {
            alphaImage = new Bitmap(currentImage);
        }

        BitmapData bmData = alphaImage.LockBits(new Rectangle(0, 0, alphaImage.Width, alphaImage.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        const int bytesPerPixel = 4;
        const int alphaPixel = 3;
        int stride = bmData.Stride;

        unsafe
        {
            byte* pixel = (byte*)(void*)bmData.Scan0;


            for (int y = 0; y < currentImage.Height; y++)
            {
                int yPos = y * stride;
                for (int x = 0; x < currentImage.Width; x++)
                {
                    int pos = yPos + (x * bytesPerPixel); 
                    pixel[pos + alphaPixel] = alphaByte;
                }
            }
        }

        alphaImage.UnlockBits(bmData);

        return alphaImage;
    }

Here is some code that adds an alpha channel to an image. If you want 50% alpha, you would set 128 as the alpha argument. Note this creates a copy of the bitmap...

    public static Bitmap AddAlpha(Bitmap currentImage, byte alpha)
    {
        Bitmap alphaImage;
        if (currentImage.PixelFormat != PixelFormat.Format32bppArgb)
        {
            alphaImage = new Bitmap(currentImage.Width, currentImage.Height, PixelFormat.Format32bppArgb);
            using (Graphics gr = Graphics.FromImage(tmpImage))
            {
                gr.DrawImage(currentImage, 0, 0, currentImage.Width, currentImage.Height);
            }
        }
        else
        {
            alphaImage = new Bitmap(currentImage);
        }

        BitmapData bmData = alphaImage.LockBits(new Rectangle(0, 0, alphaImage.Width, alphaImage.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

        const int bytesPerPixel = 4;
        const int alphaPixel = 3;
        int stride = bmData.Stride;

        unsafe
        {
            byte* pixel = (byte*)(void*)bmData.Scan0;


            for (int y = 0; y < currentImage.Height; y++)
            {
                int yPos = y * stride;
                for (int x = 0; x < currentImage.Width; x++)
                {
                    int pos = yPos + (x * bytesPerPixel); 
                    pixel[pos + alphaPixel] = alphaByte;
                }
            }
        }

        alphaImage.UnlockBits(bmData);

        return alphaImage;
    }
白鸥掠海 2024-08-11 06:52:30

您不能将其替换为实际上具有 50% 透明度的另一张图像吗?

You cant just swap it out for another image that does in fact have a 50% transparency?

温柔少女心 2024-08-11 06:52:30

您可以使用.LockBits获取指向图像像素值的指针,然后更改每个像素的alpa值。看这个问题:
来自另一张图像的 Gdiplus 遮罩图像

You can use .LockBits to get a pointer to the pixel values of the image, and then change the alpa value of each pixel. See this question:
Gdiplus mask image from another image

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