【问题标题】:WPF: Solid color ImageSourceWPF:纯色图像源
【发布时间】:2011-01-10 01:54:41
【问题描述】:

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

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

【问题讨论】:

    标签: wpf image colors imagesource


    【解决方案1】:

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

    // XAML
    <Grid Background="Black">
      <Image x:Name="imgDisplay"/>
    </Grid>
    
    // C#
    imgDisplay.Source = null;
    // -- OR --
    imgDisplay.Visibility = Visibility.Collapsed;
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-06
        • 2010-10-01
        • 2016-06-17
        • 1970-01-01
        相关资源
        最近更新 更多