c# - Windows 8 XAML : Tint an Image object

标签 c# xaml windows-runtime windows-store-apps winrt-xaml

我有一个图像对象。图像源是具有透明度的白色图形(准确地说是圆角矩形)。我想要的是能够在不同的地方重复使用相同的图像,但将图像着色为不同的颜色。到目前为止,我发现的最佳建议是在顶部绘制一个半透明的矩形,但这将显示在透明度所在的角上。谁能提出更好的选择?

最佳答案

使用 BitmapDecoder,您可以获得一个包含所有像素数据的字节数组,然后您可以通过偏移一些 ARGB 值手动更改它,并将其保存在一个新的 WriteableBitmap 中:

var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
    using (IRandomAccessStream ras = await file.Openasync(FileAccessMode.Read))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras);
        PixelDataProvider provider = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
        byte[] pixels = provider.DetachPixelData();
        for (int i = 0; i < pixels.Length; i += 4)
        {
            pixels[i] = (byte)(pixels[i] + 72);
        }
        WriteableBitmap bitmap = new WriteableBitmap((int)decoder.OrientedPixelWidth, (int)decoder.OrientedPixelHeight);
        using (Stream stream = bitmap.PixelBuffer.AsStream())
        {
            await stream.WriteAsync(pixels, 0, pixels.Length);
        }
        image.Source = bitmap;
    }
}

关于c# - Windows 8 XAML : Tint an Image object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19233084/

相关文章:

c# - 当应用程序在 WP 8.1 商店应用程序中恢复时,应用程序恢复事件不会触发

c# - 计算可能的组合数量 - C#

c# - 多语言开发

c# - 如何在不同线程中处理视频帧(C#)

wpf - 如何在 XAML 中绘制一个三等分的圆?

windows-8 - 如何安装 Windows 8 应用程序而不提交到商店

c# - 使用 c# 在 winrt 中使用 SQLiteConnection 的问题?

c# - 使用我的项目在 GAC 中正常部署程序集?

c# - 如何确定gridview中被点击的项目

c# - MVVM 在单击时更改网格的背景颜色