Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I draw lines and some lines skip pixels leaving them empty.

It is a serious problem when i wanna create cells.

My goal is to draw a grid.

How to solve the problem?

Screenshot[^]
XML
<Canvas Background="Black" RenderOptions.EdgeMode="Aliased" SnapsToDevicePixels="True">
        <Line X1='30' Y1='30' X2='100' Y2='30' Stroke="White"/>
        <Line X1='30' Y1='31' X2='100' Y2='31' Stroke="White"/>
        <Line X1='30' Y1='32' X2='100' Y2='32' Stroke="White"/>
        <Line X1='30' Y1='33' X2='100' Y2='33' Stroke="White"/>
        <Line X1='30' Y1='34' X2='100' Y2='34' Stroke="White"/>
        <Line X1='30' Y1='35' X2='100' Y2='35' Stroke="White"/>
        <Line X1='30' Y1='36' X2='100' Y2='36' Stroke="White"/>
        <Line X1='30' Y1='37' X2='100' Y2='37' Stroke="White"/>
        <Line X1='30' Y1='38' X2='100' Y2='38' Stroke="White"/>
    </Canvas>


The next code tries to draw a grid, but the grid comes to be messed as distances between lines differ due to DPI independence:

Screenshot[^]
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    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.SnapsToDevicePixels = true;

        Shape();
    }

    double i = 0;

    private void Shape()
    {
        canvas.Children.Clear();

        for (double i = 0; i < 20; i += 2)
        {
            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);
        }
    }
}
Posted
Updated 4-Aug-15 22:18pm
v12
Comments
Kornfeld Eliyahu Peter 4-Aug-15 9:46am    
Why not to draw a filled rectangle instead of 8 parallel lines?
Ziya1995 4-Aug-15 10:04am    
My end goal is to draw a grid by many 1 pixel lines.
it is done so to show the problem for plain.
Kornfeld Eliyahu Peter 4-Aug-15 10:05am    
The problem (my) that I can't see any problem...
You should post some code that reproduces the problem without any problem...
Ziya1995 4-Aug-15 10:08am    
Screenshot:
https://drive.google.com/file/d/0B4MmPdhmZzQAOGpXMUZEdDNCMlE/view?usp=sharing

The problem is i can't make a grid by 1 pixel lines nice and crisp, some lines skip leaving empty places and it results in a messed grid.

I just wanna draw a grid, how can i do it?
WPF is one the best GUI frameworks and i can't draw just a grid, how can i?
Kornfeld Eliyahu Peter 4-Aug-15 10:21am    
can you change your DPI - for test - to 96?
what is your current DPI?

1 solution

The next code solves the problem making shapes to be drawn in physical screen pixels:


C#
canvas.RenderTransform = new MatrixTransform(
PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice);


The fixed version of the code i wrote in the question:
C#
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        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.SnapsToDevicePixels = true;

            canvas.RenderTransform = new MatrixTransform(
PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice);

            Shape();
        }

        double i = 0;

        private void Shape()
        {
            canvas.Children.Clear();

            for (double i = 0; i < 20; i += 2)
            {
                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);
            }
        }
    }


Codeproject lags.

Source[^].
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900