0

I created a Canvas with fixed Width and Height (256x256). Next to it I put an Image control displaying a 256x256 texture without stretching. How is it possible that actual sizes of both controls on the screen differ so much?

Here - is the screenshot illustrating size mismatch.

And here - the TestLines256.png in case someone liked to check it (and maybe point out that I'm stupid and this texture is 180x180 and not 256x256 as I claimed).

XAML with creation of the controls:

<Window x:Class="OversizedQuad.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" SizeToContent="WidthAndHeight">
<StackPanel Orientation="Horizontal">
    <Canvas Width="256" Height="256" Background="Red" />
    <Image Stretch="None" Source="TestLines256.png" />
</StackPanel>

pbalaga
  • 2,018
  • 1
  • 21
  • 33

1 Answers1

4

WPF has Device Independent Pixel. Thus the size of pixel is different based on DPI settings. I guess for that reason the image size and Canvas size differs.

Check this :

http://www.dotnetfunda.com/articles/article882-wpf-tutorial--a-beginning--1-.aspx

abhishek
  • 2,975
  • 23
  • 38
  • Thank you! But can I force a certain control in my application (specifically this canvas) to keep pixel-based size I provided through Width and Height properties? The first idea is to create a CustomControl, trace width and height changes and finally recalculate a new size based on DPI value. – pbalaga Sep 05 '10 at 20:43
  • No idea. I think you need to mimic the GDI control pixel size algorithm and resize your canvas accordingly. – abhishek Sep 07 '10 at 10:58