使用透明度键时标签上的边框

我有一个winform(带透明度键)以使其透明。 但是当我添加标签时,我会在标签周围出现这个“fushia”边框。 我知道这是因为控件没有“设置”背景。 但有没有办法删除这个“边界”。

表格设置为背景紫红色(透明度键Fuchsia),标签设置为透明。 尝试将它绘制到具有相同结果的面板上。

我错过了什么?

看起来如何, http://oi62.tinypic.com/2wq9z7c.jpg

public class CLabel : Label { protected override CreateParams CreateParams { get { CreateParams parms = base.CreateParams; parms.ExStyle |= 0x20; return parms; } } public CLabel() { this.SetStyle(ControlStyles.Opaque, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false); } protected override void OnPaintBackground(PaintEventArgs e) { // null } GraphicsPath GetStringPath(RectangleF rect, StringFormat format) { GraphicsPath Path = new GraphicsPath(); Path.AddString(this.Text, this.Font.FontFamily, (int)Font.Style, this.Font.Size, rect, format); return Path; } protected override void OnPaint(PaintEventArgs e) { RectangleF rect = this.ClientRectangle; Font font = this.Font; StringFormat format = StringFormat.GenericDefault; using (GraphicsPath path = GetStringPath(rect, format)) { SmoothingMode sm = e.Graphics.SmoothingMode; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; Brush b = new SolidBrush(Color.White); e.Graphics.FillPath(b, path); e.Graphics.DrawPath(Pens.Black, path); b.Dispose(); } } } public partial class Dy : Form { protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; cp.ExStyle |= 0x80; return cp; } } public Dy() { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.StartPosition = FormStartPosition.Manual; // Here I add the label CLabel p = new CLabel{ Location = new Point(768, 702), Text = "25", AutoSize = false, Size = new Size(50,50), TextAlign = ContentAlignment.MiddleCenter, BackColor = Color.Transparent, ForeColor = Color.White, BorderStyle = BorderStyle.None, Font = new Font("Segoe UI", 30, FontStyle.Bold) }; } } 

看起来像抗锯齿的文物。

你可以尝试改变

 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 

 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; 

或者你可以完全关闭抗锯齿:

 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; 

编辑:我已将其移出评论,因为它可能会帮助一些人:

这是一个额外的AA-artefacts ..

AA将始终创建插值的像素,这将始终导致像素略微错过透明度键颜色。

显然,如果不使用AA,它们就会消失; 但如果真的需要一些平滑,这个小技巧可能有助于改善结果:

通常TransparencyKey使用像FuchsiaHotPink这样的粗糙颜色来完成。 这些很少使用,因此它们不太可能切割孔,但当它们透过半透明AA像素时,它们是令人讨厌的。 如果你使用较少刺激性的颜色,这种效果可以不引人注目。

你只需要确保你没有在其他地方使用它。 通过Color.FromArgb(255, r,g,b)选择一个颜色,选择3个唯一的值,这些值在其他地方不会使用,但会与前景混合,后者更重要。 饱和度非常低,根据您的图形,中等亮度可能会有所帮助..

即使它们混合得很好, Black或中性灰也不是很好的选择,因为它们经常被使用。 但是选择像ARGB(255,7,11,5)这样几乎是黑色的东西,很有可能在没有用于输出图形和切割孔的情况下进行混合。

最好的结果是你可以使用接近背景颜色的东西。 如果你不能预见它,这个技巧往往会失败,但是……