透明化 [英] Erase to Transparency

查看:110
本文介绍了透明化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图将32x32的块恢复为透明,但是每次尝试将其设置为透明时,它只会保留已有的内容,我想将图像上的内容擦除为透明的,这是我尝试的代码

So I'm trying to make a 32x32 block back to transparent but everytime I try to set it to transparent it just keeps what ever is already there, I want to erase what's on the image to transparent, here's my code I tried.

    public Bitmap erase_tile(Bitmap bitmap, int x, int y)
    {
        Graphics device = Graphics.FromImage(bitmap);

        Brush brush = new SolidBrush(Color.FromArgb(0, Color.White));

        device.FillRectangle(brush, new Rectangle(x * 32, y * 32, 32, 32));
        return bitmap;
    }

推荐答案

所有透明度将通过Bitmap类的功能来实现. Graphics类适用于 drawing ,而绘画Color.Transparent本质上是无操作的.

All transparency is going to be accomplished via functionality on the Bitmap class. The Graphics class is geared toward drawing, and drawing Color.Transparent is essentially a no-op.

您可以将Bitmap.SetPixel()Color.Transparent结合使用来设置单个像素.

You can use Bitmap.SetPixel() with Color.Transparent to set individual pixels.

或者您可以执行类似的操作,在其中使用Graphics绘制虚拟颜色,然后指示位图用作透明颜色.

Or you can do something like this, where you use Graphics to paint a dummy color which you will then instruct the bitmap to use as the transparent color.

using (var graphics = Graphics.FromImage(bmp))
{
    graphics.FillRectangle(Brushes.Red, 0, 0, 64, 64);
    graphics.FillRectangle(Brushes.Magenta, 16, 16, 32, 32);
}
bmp.MakeTransparent(Color.Magenta);

这篇关于透明化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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