WPF:纯色图像源

发布于 2024-08-19 08:35:22 字数 141 浏览 4 评论 0原文

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

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

In my app I display images I get from external DLLs, by binding the Source of an Image control to an ImageSource.

This works well, but sometimes I don't get any data from my DLL and I'd like to just show a black image. In that case, how do I create an ImageSource which contains just a solid color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墟烟 2024-08-26 08:35:22

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

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

// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;

An alternative method is to give a background color, and not show the image.

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

// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;
温折酒 2024-08-26 08:35:22

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

<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>

for example you have image somewhere in template, which binds to some property Photo. In case of failure you can return null value.

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

And somewhere in resources you need

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文