首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF:纯色ImageSource

WPF:纯色ImageSource
EN

Stack Overflow用户
提问于 2010-01-28 01:37:39
回答 2查看 3K关注 0票数 2

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-28 01:50:11

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

代码语言:javascript
复制
// XAML
<Grid Background="Black">
  <Image x:Name="imgDisplay"/>
</Grid>

// C#
imgDisplay.Source = null;
// -- OR --
imgDisplay.Visibility = Visibility.Collapsed;
票数 -1
EN

Stack Overflow用户

发布于 2010-01-28 01:49:33

例如,在模板中的某处有图像,它绑定到一些属性Photo。在失败的情况下,您可以返回空值。

代码语言:javascript
复制
<Image Source="{Binding Path=Photo, IsAsync=True, TargetNullValue={StaticResource EmptyImageDrawing}}"/>

在你需要的资源中的某个地方

代码语言:javascript
复制
<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>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2149034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档