2013-04-02 47 views
3

我想在GDI +(C#)中绘制自定义投影。我一直有这种尴尬的样子: Preview在GDI中的两个形状之间的空白+

我想我可以解决它通过添加一个像素弧的宽度,但然后形状重叠。 Preview http://puu.sh/2sv2N

我完全不知道是什么导致这个小块白色。

这里是我的代码:

Rectangle drawRect = new Rectangle(10, 10, 400, 400); 
radius = 10; 
// TOP // 
Rectangle topRect = new Rectangle(drawRect.X + radius, drawRect.Y, drawRect.Width - 2 * radius, radius); 
LinearGradientBrush topBrush = new LinearGradientBrush(new Rectangle(topRect.X, topRect.Y - 1, topRect.Width,topRect.Height + 2), firstColor, secondColor, 270f); 
g.FillRectangle(topBrush, topRect); 
topBrush.Dispose(); 

// LEFT // 
Rectangle leftRect = new Rectangle(drawRect.X, drawRect.Y + radius, radius, drawRect.Height - 2 * radius); 
LinearGradientBrush leftBrush = new LinearGradientBrush(new Rectangle(leftRect.X - 1, leftRect.Y, leftRect.Width + 2, leftRect.Height), firstColor, secondColor, 180f); 
g.FillRectangle(leftBrush, leftRect); 
leftBrush.Dispose(); 

// TOP LEFT // 
GraphicsPath topLeftPath = new GraphicsPath(); 
topLeftPath.StartFigure(); 
topLeftPath.AddArc(new Rectangle(drawRect.X, drawRect.Y, 2 * radius, 2 * radius), 180, 90); 
topLeftPath.AddLine(new Point(drawRect.X + radius, drawRect.Y), new Point(drawRect.X + radius, drawRect.Y + radius)); 
topLeftPath.AddLine(new Point(drawRect.X + radius, drawRect.Y + radius), new Point(drawRect.X, drawRect.Y + radius + 1)); 
topLeftPath.CloseFigure(); 
PathGradientBrush topLeftBrush = new PathGradientBrush(topLeftPath); 
topLeftBrush.CenterPoint = new PointF(drawRect.X + radius, drawRect.Y + radius); 
topLeftBrush.CenterColor = firstColor; 
topLeftBrush.SurroundColors = new Color[] { secondColor }; 
g.FillPath(topLeftBrush, topLeftPath); 
topLeftBrush.Dispose(); 
topLeftPath.Dispose(); 

在此先感谢

+0

无题评论:您是否知道在基于XAML的技术(如WPF或WinRT)中使用两行XAML可以实现同样的效果?我想我必须说GDI不支持“好”的东西。 –

+0

是的,我知道我应该退出GDI +并只使用WPF,但我不能只重做我的整个解决方案。 –

+1

不过,您可以通过'ElementHost'将WPF内容集成到现有的Winforms应用程序中。在WPF中做事情要容易得多,然后以这种方式整合它们,而不是试图通过winform来破解它,从而获得体面的东西。 –

回答

1

感谢LarsTech,我得到了有效的解决方案。

e.Graphics.PixelOffsetMode = PixelOffsetMode.Half; 

完成了这项工作。

谢谢!

+0

让拉尔斯发布这个答案,然后接受他的答案。这将奖励他的贡献。 –

相关问题