Note: The code was posted buggy due to the forum, fix it before to use.
Goal: To scale to fit a target DPI.
Specific code:
canvas.RenderTransform = new MatrixTransform(
PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice);
Code example:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
<pre><code> Canvas canvas;
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
canvas = new Canvas();
canvas.Background = Brushes.Black;
this.Content = canvas;
RenderOptions.SetEdgeMode(canvas, EdgeMode.Aliased);
canvas.RenderTransform = new MatrixTransform(
</code>
PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice);
Shape();
}
int i = 0;
int add = 2;
private void Shape()
{
canvas.Children.Clear();
for (int i = 0; i < 20 * add; i += add)
{
Line line = new Line();
line.X1 = 30;
line.Y1 = 30 + i;
line.X2 = 100;
line.Y2 = 30 + i;
line.Stroke = Brushes.White;
line.StrokeThickness = 1;
canvas.Children.Add(line);
}
}
}</pre>
Codeproject
source[
^].
Msdn
source[
^].