我怎样才能在XAML图片来显示他们的实际大小? [英] How can I get images in XAML to display as their actual size?

查看:277
本文介绍了我怎样才能在XAML图片来显示他们的实际大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 27×27 像素,我在WPF正在显示图像,但它显示的放大窗口的大小



我怎样才能得到它,以显示其实际大小?





XAML:

 <窗口x:类=TestImage23434.Window1
的xmlns =http://schemas.microsoft。 COM / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300宽=300>
< StackPanel的X:名称=MainStackPanel/>
< /窗GT;



代码背后:



 使用System.Windows;使用System.Windows.Controls的
;
使用System.Windows.Media.Imaging;
使用系统;

命名空间TestImage23434
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;

TextBlock的TB =新的TextBlock();
tb.Text =上述形象;
MainStackPanel.Children.Add(TB);

图片IMG =新的图像();
img.Source =新的BitmapImage(新的URI(@C:\test\circle.png));
img.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left;
MainStackPanel.VerticalAlignment = VerticalAlignment.Top;
MainStackPanel.Children.Add(IMG);

TextBlock的TB2 =新的TextBlock();
tb2.Text =下面的图片;
MainStackPanel.Children.Add(TB2);
}
}
}


解决方案

img.Stretch = Stretch.None



默认情况下,的Stretch财产均匀的值,其中调整图像大小,以填补所有可用空间。


I have a 27 x 27 pixel image that I am displaying in WPF but it displays larger than the size of the window.

How can I get it to display its actual size?

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

By default, the Stretch property has a value of Uniform, which resizes the image to fill all available space.

这篇关于我怎样才能在XAML图片来显示他们的实际大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆