我正在尝试在物理设备像素边界上绘制网格,但它无法正常工作。要获取随附的示例代码,第一行是左边的一个像素,第二行是右边的一个像素等等。我想在没有96像素到英寸的设备上生成像素完美的网格。在这方面的任何帮助将不胜感激....
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
var m = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice;
double dpiFactor = 1 / m.M11;
var pen = new Pen(Brushes.Black, 1 * dpiFactor);
double halfPenWidth = pen.Thickness / 2.0;
for (int x = 1; x <= Consts.GridSize - 1; x++)
{
var p1 = new Point(Consts.CellSize * x, 0);
var p2 = new Point(Consts.CellSize * x, Height - 1);
var rect = new Rect(p1, p2);
var guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rect.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rect.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth);
drawingContext.PushGuidelineSet(guidelines);
drawingContext.DrawRectangle(null, pen, rect);
//drawingContext.DrawLine(pen, p1, p2);
drawingContext.Pop();
}
}