ImageSource 上的转换器 - 将图像转换为灰度

发布于 2024-10-12 17:37:44 字数 2010 浏览 4 评论 0原文

我对图像控件有多重绑定。我绑定了两个属性,一个是 bool(IsLogged) 类型,一个是 Uri (ProfilePhoto) 类型。

XAML:

                                <Image.Source >
                                    <MultiBinding Converter="{StaticResource avatarConverter}">
                                        <Binding Path="ProfilePhoto"></Binding>
                                        <Binding Path="StatusInfo.IsLogged"></Binding>
                                    </MultiBinding>
                                </Image.Source>
                            </Image>

我创建转换器,如果属性 IsLogged 为 false,则将 BitmapImage 转换为灰度。

它看起来像这样:

public class AvatarConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var image = values[0] as BitmapImage;

        string s = values[1].ToString();

        bool isLogged = System.Convert.ToBoolean(s);

        if (!isLogged)
        {
            try
            {
                if (image != null)
                {
                    var grayBitmapSource = new FormatConvertedBitmap();
                    grayBitmapSource.BeginInit();
                    grayBitmapSource.Source = image;
                    grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                    grayBitmapSource.EndInit();
                    return grayBitmapSource;
                }
                return null;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        return image;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果我只绑定 BitmapImage 的图像源属性类型,效果很好,但我需要绑定 Uri 的属性类型。

我担心转换器中的创建变量 BitmapImage 并作为源使用 Uri。 返回此变量作为图像源。我认为这不是理想的方式。也许我错了。

您认为

一些优雅的解决方案是什么?

I have multibinding on Image control. I bind two properties one is type of bool(IsLogged) and one is typeof Uri (ProfilePhoto).

XAML:

                                <Image.Source >
                                    <MultiBinding Converter="{StaticResource avatarConverter}">
                                        <Binding Path="ProfilePhoto"></Binding>
                                        <Binding Path="StatusInfo.IsLogged"></Binding>
                                    </MultiBinding>
                                </Image.Source>
                            </Image>

I create converter, which convert BitmapImage to gray scale if property IsLogged is false.

It look like this:

public class AvatarConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        var image = values[0] as BitmapImage;

        string s = values[1].ToString();

        bool isLogged = System.Convert.ToBoolean(s);

        if (!isLogged)
        {
            try
            {
                if (image != null)
                {
                    var grayBitmapSource = new FormatConvertedBitmap();
                    grayBitmapSource.BeginInit();
                    grayBitmapSource.Source = image;
                    grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                    grayBitmapSource.EndInit();
                    return grayBitmapSource;
                }
                return null;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        return image;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

It works good if only I bind on image source property type fo BitmapImage, but I need bind property type of Uri.

I have a fear of the creation variable BitmapImage in converter and as source use Uri.
An return this variable as Source of image. I think this is not ideal way. Maybe I am wrong.

What is your opinion

Some elegant solution?

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

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

发布评论

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

评论(1

白云悠悠 2024-10-19 17:37:44

虽然您可以使用转换器来完成此操作,但还有更好的选择:使用着色器效果。您将在此上找到 GreyscaleEffect 的实现页面

<Style x:Key="grayedIfNotLogged" TargetType="Image">
    <Style.Triggers>
        <DataTrigger Binding="{Binding StatusInfo.IsLogged}" Value="False">
            <Setter Property="Effect">
                <Setter.Value>
                    <fx:GrayscaleEffect />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<Image Source="..." Style="{StaticResource grayedIfNotLogged}" />

Although you can do it with a converter, there is a much better option: using a shader effect. You'll find an implementation of a GreyscaleEffect on this page.

<Style x:Key="grayedIfNotLogged" TargetType="Image">
    <Style.Triggers>
        <DataTrigger Binding="{Binding StatusInfo.IsLogged}" Value="False">
            <Setter Property="Effect">
                <Setter.Value>
                    <fx:GrayscaleEffect />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<Image Source="..." Style="{StaticResource grayedIfNotLogged}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文