c# - 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>
如您所见,我尝试使整个位图变黑。不幸的是,我得到的结果是什么都没有显示。它只是应该显示我的位图的地方的一个空白区域。
解决方案
推荐阅读
- json - 如何在 JSON 文件上运行的 GraphQL 上应用过滤器?
- laravel - 批量更新在 pgsql 数据库 laravel 中不起作用
- python - 离线模式linux下pip安装要求
- android - 是否不仅可以在 Firebase Analytics 中查看过去 30 分钟内的事件?
- r - 数据帧的每 n 行和 n 列的总和
- apache-spark - 没有活动时杀死火花流作业
- angular - WebPack:无效的配置对象。Webpack 已使用与 API 模式不匹配的配置对象进行初始化
- visual-c++ - OnKillFocus 被调用了两次
- python-3.x - 通过 Regex 和 df.str.contains 在单行数据框中查找不同的电子邮件 ID
- javascript - 如何在 mocha / sinon 中测试条件表达式