[英]How can I get images in XAML to display as their actual size?
I have a 27 x 27 pixel image that I am displaying in WPF but it displays larger than the size of the window . 我有一个27 x 27像素的图像,该图像在WPF中显示,但显示的尺寸大于窗口的尺寸。
How can I get it to display its actual size? 如何获得它的实际尺寸?
alt text http://www.deviantsart.com/upload/m20dk6.png 替代文字http://www.deviantsart.com/upload/m20dk6.png
XAML: XAML:
<Window x:Class="TestImage23434.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="MainStackPanel"/>
</Window>
Code Behind: 背后的代码:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System;
namespace TestImage23434
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TextBlock tb = new TextBlock();
tb.Text = "above image ";
MainStackPanel.Children.Add(tb);
Image img = new Image();
img.Source = new BitmapImage(new Uri(@"C:\test\circle.png"));
img.HorizontalAlignment = HorizontalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
MainStackPanel.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.Children.Add(img);
TextBlock tb2 = new TextBlock();
tb2.Text = "below image";
MainStackPanel.Children.Add(tb2);
}
}
}
img.Stretch = Stretch.None
默认情况下, Stretch属性的值是Uniform
,它会调整图像的大小以填充所有可用空间。
None of these tips were working for me. 这些技巧都不适合我。 Some binding tricks is what got me going. 一些约束技巧使我前进。
<Image Source="yourPic.png" Stretch="UniformToFill"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}" />
You could probably set Strech to None but this worked for my app using .net 4.5.1. 您可能将Strech设置为None,但这对使用.net 4.5.1的应用程序有效。
img.HorizontalAlignment = HorizontalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
img.Stretch = Stretch.None;
The StackPanel will stretch to fill/overflow whatever container it's in unless its size is specified. 除非指定其大小,否则StackPanel会拉伸以填充/溢出容器中的任何容器。 Since you aren't setting margins or alignments on anything, the default behavior for everything is Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
, or simply put "Stretch to fill". 由于您未在任何内容上设置边距或对齐方式,因此所有内容的默认行为是Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
,或简单地将“ Stretch to fill”填充。 Aligning/positioning your elements will fix this issue. 对齐/定位元素将解决此问题。
EDIT: Added img.Stretch = Stretch.None;
编辑:添加img.Stretch = Stretch.None;
, also I built a sample app and tested it... it works. ,我还构建了一个示例应用程序并对其进行了测试...它可以工作。
It could also be your image's DPI property in its metadata. 它也可以是图像元数据中图像的DPI属性。 It should be 96. https://stackoverflow.com/a/4965404/659326 它应该是96。https ://stackoverflow.com/a/4965404/659326
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.