开发者

Convertor on ImageSource - convert image to grayscale

开发者 https://www.devze.com 2023-02-05 12:16 出处:网络
I have multibinding on Image control. I bind two properties one is type of bool(IsLogged) and one is typeof Uri (ProfilePhoto).

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, Typ开发者_Python百科e[] 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?


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}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号