如何在Windows Phone 8.1 RT中编辑BitmapImage?

时间:2015-12-18 15:56:44

标签: c# windows-phone-8.1

我必须在像素级编辑bitmapimage。但是Windows Phone的Writeablebitmap Class没有WritePixels功能。

1 个答案:

答案 0 :(得分:1)

我希望这是你正在寻找的东西。 我有同样的问题,并设法解决它。如果您需要其他解释,请发表评论。

private static async Task<BitmapImage> ConvertImage(byte[] imageSource)
{
    if (imageSource == null)
    {
        return null;
    }

    MemoryStream memoryStream = new MemoryStream(imageSource);

    using (IRandomAccessStream randomAccessStream = memoryStream.AsRandomAccessStream())
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(randomAccessStream);
        PixelDataProvider provider = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
        byte[] pixels = provider.DetachPixelData();

        //Each Pixel is composed of 4 bytes [0]: Blue, [1]: Green, [2]: Red, [3] Alpha
        //Do Here your magic with this pixels byte array

        using (InMemoryRandomAccessStream memoryRandomAccessStream = new InMemoryRandomAccessStream())
        {
            var imageBytes = await EncodeImageBytes(memoryRandomAccessStream, decoder, pixels);
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                BitmapImage image = new BitmapImage();
                await stream.WriteAsync(imageBytes.AsBuffer());
                stream.Seek(0);
                image.SetSource(stream);
                return image;
            }
        }
    }
}

private static async Task<byte[]> EncodeImageBytes(InMemoryRandomAccessStream memoryRandomAccessStream, BitmapDecoder decoder, byte[] pixels)
{
    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memoryRandomAccessStream);
    encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.PixelWidth, decoder.PixelHeight, 96, 96, pixels);

    await encoder.FlushAsync();

    var imageBytes = new byte[memoryRandomAccessStream.Size];

    await memoryRandomAccessStream.ReadAsync(imageBytes.AsBuffer(), (uint) memoryRandomAccessStream.Size, InputStreamOptions.None);

    return imageBytes;
}