C# 擦除为透明
所以我尝试将32x32块恢复为透明,但每次我尝试将其设置为透明时,它只保留已经存在的内容,我想将图像上的内容擦除为透明,这是我尝试的代码C# 擦除为透明,c#,gdi+,C#,Gdi+,所以我尝试将32x32块恢复为透明,但每次我尝试将其设置为透明时,它只保留已经存在的内容,我想将图像上的内容擦除为透明,这是我尝试的代码 public Bitmap erase_tile(Bitmap bitmap, int x, int y) { Graphics device = Graphics.FromImage(bitmap); Brush brush = new SolidBrush(Color.FromArgb(0, Color.W
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;
}
所有透明度都将通过
位图
类上的功能来实现。Graphics
类面向绘图,而绘图Color.Transparent
本质上是一个禁忌
您可以使用Bitmap.SetPixel()
和Color.Transparent
来设置单个像素
或者您可以这样做,使用图形
绘制虚拟颜色,然后指示位图将其用作透明颜色
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);
当我在寻找相同的解决方案时,我无法找到确切的答案,因此经过一些实验后,我遇到了
SetCompositingMode
,它成功了(有关详细信息,请参阅)
这里是C++中的一个工作代码来演示该方法(它需要在C语言中重新使用一些调整):
您是否尝试过
Graphics.Clear(Color.Transparent)
?透明是一种“假”,它采用其背后对象的颜色取决于您是绘制透明颜色还是将像素设置为透明颜色。我不完全确定那些Graphics.Clear()
中的哪一个会清除。@Michael\u Gunter,Graphics.Clear清除整个图像我只想清除图像的一部分32x32。此代码没有严重错误。所以这可能是你下一步要做的。嗯,我明白了,我可以测试一下。我正在创建图层,现在给我10分钟,我会试试。谢谢:D我会在一秒钟内给它一个测试:)它工作:)我会给你+1,但我还没有15次。谢谢你!我想你可以在没有任何声誉的情况下把我的答案标记为正确的。
void SetTransparent(Gdiplus::Image* image, IN INT x, IN INT y, IN INT width, IN INT height)
{
Gdiplus::Graphics graph(image);
graph.SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
Gdiplus::SolidBrush transparent(0);
graph.FillRectangle(&transparent, x, y, width, height);
}