WPF Colorize Grayscale BitmapImage

时间:2018-04-02 12:39:33

标签: c# wpf colors

如何使用MVVM模式为WPF(最好)中的灰度BitmapImage着色?

这是我在DataTemplate

中使用的ItemsControl
<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding Image}">
                <Image.RenderTransform>
                    <ScaleTransform RenderOptions.BitmapScalingMode="HighQuality" ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}" />
                </Image.RenderTransform>
            </Image>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Canvas.Left" Value="{Binding X}" />
            <Setter Property="Canvas.Top" Value="{Binding Y}" />
            <Setter Property="Canvas.ZIndex" Value="{Binding Z}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

我看过MatrixTransform,但似乎只用于倾斜/旋转图像?

这是我在Java中用来实现同样的目标:

public static void colorize(BufferedImage image, Color maskColor)
{
    for (int x = 0; x < image.getWidth(); x++)
    {
        for (int y = 0; y < image.getHeight(); y++)
        {
            int pixel = image.getRGB(x, y);
            if( (pixel >> 24) == 0x00 )
                continue;

            Color color = new Color(pixel);
            float red   = (color.getRed()   / 255.0F) * (maskColor.getRed()   / 255.0F);
            float green = (color.getGreen() / 255.0F) * (maskColor.getGreen()   / 255.0F);
            float blue  = (color.getBlue()  / 255.0F) * (maskColor.getBlue()   / 255.0F);

            color = new Color(red, green, blue);
            int rgb = color.getRGB();
            image.setRGB(x, y, rgb);
        }
    }
}

将灰度图像着色的最佳方法是什么?我对WPF比较陌生。

0 个答案:

没有答案