首页 > 解决方案 > WPF - 显示以 BitmapImage 作为源的图像时出现问题

问题描述

在使用 WPF 创建应用程序时,我一直使用 MVVM 模型,并且与显示/修改位图相关的所有内容都运行良好,但是这次我决定选择其他方式,因为我想将位图的宽度和高度调整为应该包含它的控件,但有些东西在这里不起作用。

我使用一个在 Load 上执行的事件,因为我想获得前面所说的实际高度和宽度。

我的部分代码:

    private BitmapImage scene;
    public BitmapImage Scene
    {
        get { return scene; }
        set { if (scene != value) { scene = value; RaisePropertyChanged("Scene"); } }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        LoadSceneCommand = new RelayCommand(LoadScene);
        GenerateTexturedCylinderCommand = new RelayCommand(GenerateTexturedCylinder);
        GenerateTexturedSphereCommand = new RelayCommand(GenerateTexturedSphere);
        GenerateCylinderShadingCommand = new RelayCommand(GenerateCylinderShading);
        GenerateSphereShadingCommand = new RelayCommand(GenerateSphereShading);
    }

    private void GetSceneSize(object sender, RoutedEventArgs e)
    {
        object wantedNode = (Grid)this.FindName("SceneGrid");
        if (wantedNode is Grid)
        {
            Grid imageControl = wantedNode as Grid;

            sceneWidth = (int)imageControl.ActualWidth;
            sceneHeight = (int)imageControl.ActualHeight;

            try
            {
                PrepareScene();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    private void PrepareScene()
    {
        Bitmap bitmap = new Bitmap(sceneWidth, sceneHeight);

        for (int x = 0; x < sceneWidth; ++x)
            for (int y = 0; y < sceneHeight; ++y)
                bitmap.SetPixel(x, y, Color.Black);

        Scene = ImageConverters.Bitmap2BitmapImage(bitmap);
    }

xml:

<Window x:Class="_3DRendering.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Loaded="GetSceneSize"
    ResizeMode="NoResize"
    Title="MainWindow" Height="450" Width="800">

<Window.Resources>

</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="10"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="10"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="10"/>
    </Grid.RowDefinitions>

    <Grid Grid.Column="1" Grid.Row="1" x:Name="SceneGrid">
        <Image Source="{Binding Scene}" x:Name="ImageControl"/>
    </Grid>

    <ScrollViewer Grid.Column="3" Grid.Row="1" VerticalScrollBarVisibility="Auto">
        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="0 10 0 0"/>
                    <Setter Property="Padding" Value="5 5 5 5"/>
                </Style>
            </StackPanel.Resources>

            <Button Content="Textured cylinder" Command="{Binding GenerateTexturedCylinderCommand}"/>
            <Button Content="Textured sphere" Command="{Binding GenerateTexturedSphereCommand}"/>
            <Button Content="Cylinder shading" Command="{Binding GenerateCylinderShadingCommand}"/>
            <Button Content="Sphere shading" Command="{Binding GenerateSphereShadingCommand}"/>
            <Button Content="Load Scene" Command="{Binding LoadSceneCommand}"/>
            <Button Content="Save Scene" Command="{Binding }"/>

        </StackPanel>
    </ScrollViewer>
</Grid>

如您所见,我尝试使整个位图变黑。不幸的是,我得到的结果是什么都没有显示。它只是应该显示我的位图的地方的一个空白区域。

标签: c#wpfbitmap

解决方案


推荐阅读