简体   繁体   English

如何在XAML中获取图像以实际尺寸显示?

[英]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.

相关问题 如何获得与窗口大小成比例显示的图像? - How can I get images to display in proportion to the size of the window? 为什么在粘贴到 Excel 时图像显示的大小大于其实际大小,我如何才能使其以自然大小显示? - Why does an image display at greater that its actual size when pasting into Excel, and how can I get it to display at its natural size? 如何获取XAML中定义的CollectionView - How can I get the CollectionView that is defined in XAML winForms中点击size属性设置为CenterImage的pictureBox如何获取图片的实际坐标? - How can I get the actual coordinates of the image when I click on the pictureBox whose size property is set to CenterImage in winForms? 如何在XAML / C#中显示超棒的字形 - How can I display a font awesome glyph in XAML/C# 如何在RichTextBox中以格式文本显示Xaml消息 - How can I display Xaml message as formatted text in RichTextBox 如何强制ListBoxItems在XAML中内联显示 - How can I force ListBoxItems to display inline in XAML 如何在C#中显示引用的实际值 - How can I display the actual value of a reference in C# 如何显示邮件内容中的图像? - How can I display images in mail content? 如何从SQLite表中获取实际的日期时间值? - How can I get the actual datetime values from a SQLite table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM