Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将bmp加载到ContentPresenter中?_C#_Wpf_Bmp_Contentpresenter - Fatal编程技术网

C# 如何将bmp加载到ContentPresenter中?

C# 如何将bmp加载到ContentPresenter中?,c#,wpf,bmp,contentpresenter,C#,Wpf,Bmp,Contentpresenter,我有一个ContentPresenter内容,我刚刚从文件中加载了一个bmp。我希望bmp显示在ContentPresenter中,并利用缩放功能 我拥有的代码(仅显示bmp文件的路径)是: 如果不想预加载图像,则必须在主窗口上创建Dependency属性。您还必须使用。XAML和codebehind文件如下所示: XAML: <Window x:Class="TestWPFApp.MainWindow" xmlns="http://schemas.microsoft.co

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

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


如果不想预加载图像,则必须在主窗口上创建Dependency属性。您还必须使用。XAML和codebehind文件如下所示:

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);
        }
    }
}
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Controls;
命名空间TestWPFApp
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=this;
cbImageSelect.ItemsSource=new List(){“test1.bmp”、“test2.bmp”};
}
公共静态只读DependencyProperty ImageUriProperty=DependencyProperty.Register(“ImageUri”、typeof(string)、typeof(MainWindow));
公共字符串ImageUri
{
get{return(string)GetValue(ImageUriProperty);}
set{SetValue(ImageUriProperty,value);}
}
private void组合框\u SelectionChanged(对象发送者,SelectionChangedEventArgs e)
{
ImageUri=”pack://application:,,,/“+((发件人作为组合框)。选择EdItem作为字符串);
}
}
}
图像位置:test1.bmp和test2.bmp

test1.bmp和test2.bmp的属性

ContentPresenter基本上做两件事:直接显示元素,或以
数据模板定义的方式显示数据(更多详细信息请参见上的备注)
BitmapImage
不是一个元素,并且没有与之关联的特定数据模板,因此ContentPresenter返回到只显示它的
ToString

您可以创建
图像
元素并直接将内容设置为该元素,也可以通过
内容模板
或将数据模板定义为具有
数据类型的资源

内容模板:

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

资源:

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


Content.Content=bitmap
?为什么不直接使用图像类?我对图像类了解不多,认为这是正确的答案。我只是想在这个项目上尽快地整合前端,因为真正的工作是编写底层的人工智能代码;请注意UI(稍后仍将抛出)。使用Image类可以解决我所有的问题吗?这似乎解决了它:contentPresenter.Content=bitmap;这很有效。。。只是我失去了缩放和滚动的能力。我使用的示例代码如下:。我真的很感激你的帮助;谢谢这个方法非常有效!但我希望能够在运行时从菜单项的对话框加载bmp文件。我该怎么做?我不想被绑定到预加载的bmp。@zetar:我已经更新了我的答案,以反映运行时根据组合框选择加载的图像。希望您可以对菜单项和对话框进行适当的更改。
<Window.Resources>
    <DataTemplate DataType="{x:Type BitmapImage}">
        <Image Source="{Binding}" />
    </DataTemplate>
</Window.Resources>