SetColorKey 允许您在图形对象上绘制图像时选择透明背景。
但事实是,当调用 SetRemapTable() 函数时,这不起作用。
您也可以通过在“colorMaps”数组中添加一个额外的 ColorMap 来实现这一点。
这个额外的 ColorMap 应该有
- OldColor = '选择透明色'
- NewColor = 颜色.透明
然后使用扩展子调用 SetRemapTable。
下面是一个 C# 代码示例,可以轻松地将图像绘制到图形对象。
我用它来制作带有图形的游戏。
这个 void(基本的 sub)允许您将图像绘制到图形(例如 FormX.CreateGraphics())。
您可以将某些颜色替换为其他颜色,也可以选择透明颜色。
您还可以绘制指定角度(度)的图像。
public static void DrawImageToGraphics(Graphics gr, Bitmap img, Rectangle DestRect, Color[] OldColors, Color[] NewColors, Color TransparantColor, uint Angle)
{
System.Drawing.Drawing2D.Matrix lmx = new System.Drawing.Drawing2D.Matrix();
lmx.RotateAt(Angle, new PointF((DestRect.Left + DestRect.Right) / 2, (DestRect.Top + DestRect.Bottom) / 2));
gr.Transform = lmx;
System.Drawing.Imaging.ColorMap[] maps = new System.Drawing.Imaging.ColorMap[OldColors.Count() + 1];
for (int i = 0; i < OldColors.Count(); i++)
{
maps[i].OldColor = OldColors[i];
maps[i].NewColor = NewColors[i];
}
maps[OldColors.Count()].OldColor = TransparantColor;
maps[OldColors.Count()].NewColor = Color.Transparent;
System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
attr.SetRemapTable(maps);
gr.DrawImage(img, DestRect, 0, 0, img.Width, img.Height, GraphicsUnit.Point, attr);
}