如何将bmp加载到ContentPresenter中?

时间:2013-05-23 13:58:39

标签: c# wpf bmp contentpresenter

我有一个ContentPresenter内容,我刚从文件中加载了一个bmp。我希望bmp出现在ContentPresenter中并利用缩放功能。

我拥有的代码(仅显示bmp文件的路径)是:

        BitmapImage bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource = new Uri(selectedFileName);
        bitmap.EndInit();
        Content = bitmap;

2 个答案:

答案 0 :(得分:1)

如果您不想预加载图像,则必须在主窗口上创建依赖属性。您还必须使用WPF Pack URIs。 XAML和代码隐藏文件如下:

XAML:

<Window x:Class="TestWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWPFApp"
        Title="MainWindow" Height="550" Width="725">

    <Grid x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <Label Content="Select Image" Width="100" Height="30" Margin="10,10,100,10"></Label>
            <ComboBox x:Name="cbImageSelect" Height="20" Width="400" SelectionChanged="ComboBox_SelectionChanged" />
        </StackPanel>
        <ContentPresenter x:Name="contentPresenter" Width="250" Height="250" Grid.Row="1" >
            <ContentPresenter.Content>
                <Image Source="{Binding ImageUri}" Width="220" Height="220">
                </Image>
            </ContentPresenter.Content>
        </ContentPresenter>
    </Grid>
</Window>

XAML.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TestWPFApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            cbImageSelect.ItemsSource = new List<string>() { "test1.bmp", "test2.bmp" };
        }

        public static readonly DependencyProperty ImageUriProperty = DependencyProperty.Register("ImageUri", typeof(string), typeof(MainWindow));

        public string ImageUri
        {
            get { return (string)GetValue(ImageUriProperty); }
            set { SetValue(ImageUriProperty, value); }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ImageUri = "pack://application:,,,/" + ((sender as ComboBox).SelectedItem as string);
        }
    }
}

图像的位置:test1.bmp和test2.bmp

enter image description here

test1.bmp和test2.bmp的属性
enter image description here enter image description here

答案 1 :(得分:0)

ContentPresenter基本上做两件事:直接显示元素,或以DataTemplate定义的方式显示数据(the MSDN page上的注释中的更多细节)。 BitmapImage不是一个元素,并且没有与之关联的特定DataTemplate,因此ContentPresenter会回退到只显示它的ToString

您可以创建Image元素并直接向其设置内容,也可以ContentTemplateDataType的资源定义DataTemplate。

的ContentTemplate:

<ContentPresenter>
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <Image Source="{Binding}" />
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>

资源:

<Window.Resources>
    <DataTemplate DataType="{x:Type BitmapImage}">
        <Image Source="{Binding}" />
    </DataTemplate>
</Window.Resources>