0

Is there a way to implement pixel snapping in UWP from code behind ?

I know XAML controls in UWP automatically do this, but I'm using a thir party library, Win2D, to draw some lines, and pixel snapping is not default. Aligning these lines with the rest of the interface is a pain. I need to align some vertical borders drawn with Win2D with the pixel snapped vertical borders of some cells drawn inside a XAML horizontal ListView. What I would need would be a way to get the exact X and Y coordinates of the closest pixel. I need to draw a vertical / horizontal line with Win2D.DrawLine from A to B and I need to snap to pixels to prevent antialising and to align with a XAML Border.

This is picture of the actual issue. The lower raw is drawn with Win2d, The upper raw is the table header and is drawn with XAML controls.

enter image description here

I need a way to snap to pixel the lines drawn with Win2D just as XAML does this.

Bart
  • 9,925
  • 7
  • 47
  • 64
Octavian Epure
  • 1,019
  • 5
  • 19
  • 35
  • The image doesn't look, like the vertical line would be a fraction of a pixel off. It looks more like it is an entire pixel off. Although the image quality is pretty bad to be sure (PNGs are lossless, but yours looks like it has JPG artifacts; can you provide an image that didn't go through lossy conversions?). – IInspectable Aug 05 '16 at 11:19
  • @IInspectable, this is not a picture, it is a drawn area, using Win2D library. I found the solution, as recomended by win2D staff: I got the exact X,Y coordinates of the items I needed to align to and it worked perfectly. – Octavian Epure Aug 05 '16 at 11:24
  • It's a screenshot (that's still an picture, right?), and it looks blurred, which it shouldn't, as it is a PNG. So I suppose it was converted from a lossy file format along the way. I was asking for a screenshot, that doesn't exhibit compression artifacts. – IInspectable Aug 05 '16 at 11:30

2 Answers2

1

I got the exact coordinates of the items I needed to align to using this sample:

Absolute coordinates of UIElement in WinRT

Then I have simply drawn the lines to the exact coordinates.

Community
  • 1
  • 1
Octavian Epure
  • 1,019
  • 5
  • 19
  • 35
0

The alternative to your own answer might be to draw just these lines using XAML.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100