我已经创建了一个简单的测试来显示这个问题,这是使用C#winform的演示代码:
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.FromArgb(255, 41, 41, 41));
float distance = 20;
for (int i = 1; i < 25; ++i)
{
if (i <= 10)
{
using (Pen p = new Pen(Color.White, ((float)i) / 10))
{
e.Graphics.DrawLine(p, new PointF(0, i * distance + 10), new PointF(this.Width, i * distance + 10));
}
}
else if (i == 11)
{
using (Pen p = new Pen(Color.Red, 1.0f))
{
e.Graphics.DrawLine(p, new PointF(0, i * distance + 10), new PointF(this.Width, i * distance + 10));
}
}
else
{
using (Pen p = new Pen(Color.White, i-11))
{
e.Graphics.DrawLine(p, new PointF(0, i * distance + 10), new PointF(this.Width, i * distance + 10));
}
}
}
}
这是代码使用sharpdx做同样的事情:
private void renderControl_Paint(object sender, PaintEventArgs e)
{
try
{
RenderTarget2D.BeginDraw();
RenderTarget2D.Clear(new Color(41, 41, 41, 255));
float distance = 20;
for(int i=1; i<25; ++i)
{
if(i<=10)
{
RenderTarget2D.DrawLine(new RawVector2(0, i * distance + 10), new RawVector2(renderControl.Width, i * distance + 10),
SceneColorBrush,
((float)i) / 10.0f
);
}
else if (i == 11)
{
RenderTarget2D.DrawLine(new RawVector2(0, i * distance + 10), new RawVector2(renderControl.Width, i * distance + 10),
FlagSceneColorBrush,
i - 10
);
}
else
{
RenderTarget2D.DrawLine(new RawVector2(0, i * distance + 10), new RawVector2(renderControl.Width, i * distance + 10),
SceneColorBrush,
i - 11
);
}
}
RenderTarget2D.EndDraw();
}
catch (Exception ex)
{
LogException(ex);
}
}
wpf测试代码:
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawRectangle(BackgroundBrush, BackgroundPen, new Rect(0, 0, Width, Height));
float distance = 20;
for (int i = 1; i < 25; ++i)
{
if (i <= 10)
{
Pen p = new Pen( myLineBrush, ((float)i) / 10 );
drawingContext.DrawLine(p, new Point(0, i * distance + 10), new Point(this.Width, i * distance + 10));
}
else if (i == 11)
{
Pen p = new Pen(myFlagLineBrush, 1.0f);
drawingContext.DrawLine(p, new Point(0, i * distance + 10), new Point(this.Width, i * distance + 10));
}
else
{
myLineBrush.Color = Color.FromArgb(255, 255, 255, 255);
Pen p = new Pen(myLineBrush, i - 11);
drawingContext.DrawLine(p, new Point(0, i * distance + 10), new Point(this.Width, i * distance + 10));
}
}
}
你可以看到,direct2d绘制所有的线都使用两个像素,而gdi +使用一个像素。因此它使得direct2d绘制的线看起来比gid +线更粗。 我已经在wpf中测试了这段代码并使用DrawingContext绘制了线条,它与direct2d具有相同的结果。 我发现direct2d drawline函数中有一个参数。
virtual void DrawLine(
D2D1_POINT_2F point0,
D2D1_POINT_2F point1,
ID2D1Brush *brush,
FLOAT strokeWidth = 1.0f,
ID2D1StrokeStyle *strokeStyle = NULL ) = 0;
当我将行程改为小于1.0时,它只会使线条变暗,而不会使线条变得更薄。 所以我想知道,如何更改direct2d / wpf行的默认像素宽度?让它只在屏幕上像GDI +一样,谢谢。