WPF:纯色图像源

WPF:纯色图像源,wpf,image,colors,imagesource,Wpf,Image,Colors,Imagesource,在我的应用程序中,我通过将图像控件的源绑定到ImageSource来显示从外部DLL获取的图像 这工作得很好,但有时我没有从我的DLL中获取任何数据,我只想显示一个黑色图像。在这种情况下,如何创建仅包含纯色的ImageSource?例如,模板中的某个位置有图像,该图像绑定到某个属性照片。如果失败,您可以返回空值 <Image Source="{Binding Path=Photo, IsAsync=True, TargetNullValue={StaticResource EmptyIma

在我的应用程序中,我通过将图像控件的源绑定到ImageSource来显示从外部DLL获取的图像


这工作得很好,但有时我没有从我的DLL中获取任何数据,我只想显示一个黑色图像。在这种情况下,如何创建仅包含纯色的ImageSource?

例如,模板中的某个位置有图像,该图像绑定到某个属性照片。如果失败,您可以返回空值

<Image Source="{Binding Path=Photo, IsAsync=True, TargetNullValue={StaticResource EmptyImageDrawing}}"/>

在你需要的资源中

<DrawingImage
                x:Key="EmptyImageDrawing">
                <DrawingImage.Drawing>
                    <DrawingGroup>
                        <GeometryDrawing>
                            <GeometryDrawing.Brush>
                                <VisualBrush
                                    AlignmentX="Center"
                                    AlignmentY="Center"
                                    Stretch="None">
                                    <VisualBrush.Visual>
                                        <TextBlock
                                            Text="Failed to load photo"
                                            FontFamily="Calibri"
                                            FontSize="70"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Bottom"
                                            TextAlignment="Center"
                                            TextWrapping="Wrap"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </GeometryDrawing.Brush>
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry
                                    Rect="0,0,1920,1080" />
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingGroup>
                </DrawingImage.Drawing>
            </DrawingImage>

例如,模板中某处有一个图像,它绑定到某个属性照片。如果失败,您可以返回空值

<Image Source="{Binding Path=Photo, IsAsync=True, TargetNullValue={StaticResource EmptyImageDrawing}}"/>

在你需要的资源中

<DrawingImage
                x:Key="EmptyImageDrawing">
                <DrawingImage.Drawing>
                    <DrawingGroup>
                        <GeometryDrawing>
                            <GeometryDrawing.Brush>
                                <VisualBrush
                                    AlignmentX="Center"
                                    AlignmentY="Center"
                                    Stretch="None">
                                    <VisualBrush.Visual>
                                        <TextBlock
                                            Text="Failed to load photo"
                                            FontFamily="Calibri"
                                            FontSize="70"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Bottom"
                                            TextAlignment="Center"
                                            TextWrapping="Wrap"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </GeometryDrawing.Brush>
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry
                                    Rect="0,0,1920,1080" />
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingGroup>
                </DrawingImage.Drawing>
            </DrawingImage>

另一种方法是提供背景色,而不显示图像

// XAML
<Grid Background="Black">
  <Image x:Name="imgDisplay"/>
</Grid>

// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;
//XAML
//C#
imgDisplay.Source=null;
//或者--
imgDisplay.Visibility=Visibility.collazed;

另一种方法是提供背景色,而不显示图像

// XAML
<Grid Background="Black">
  <Image x:Name="imgDisplay"/>
</Grid>

// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;
//XAML
//C#
imgDisplay.Source=null;
//或者--
imgDisplay.Visibility=Visibility.collazed;