tags:

views:

43

answers:

1

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>

+3  A: 

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
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.
rook
No idea. I think you need to mimic the GDI control pixel size algorithm and resize your canvas accordingly.
abhishek