升级blit的质量(Winrt c#)

时间:2013-04-08 13:33:06

标签: c# windows-runtime blit writeablebitmapex

我实际上尝试制作一个带有多个东西的画布的独特图像。

我创建了一张我需要的图像,但图像的质量非常差。你知道我怎么升级吗?

当我保存图片时,这是我的代码的结尾:

    StorageFile filesave = await ApplicationData.Current.LocalFolder.CreateFileAsync("filesave.jpg", CreationCollisionOption.ReplaceExisting);

    Guid encoderId = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;
    await WinRTXamlToolkit.Imaging.WriteableBitmapSaveExtensions.SaveToFile(backgroundBmp, filesave, encoderId);

如果有人有想法?谢谢你的时间,问候。

1 个答案:

答案 0 :(得分:1)

这是SaveToFile方法的来源:

    /// <summary>
    /// Saves the WriteableBitmap to the given file with the specified BitmapEncoder ID.
    /// </summary>
    /// <param name="writeableBitmap">The writeable bitmap.</param>
    /// <param name="outputFile">The output file.</param>
    /// <param name="encoderId">The encoder id.</param>
    /// <returns></returns>
    public static async Task SaveToFile(
        this WriteableBitmap writeableBitmap,
        StorageFile outputFile,
        Guid encoderId)
    {
        Stream stream = writeableBitmap.PixelBuffer.AsStream();
        byte[] pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);

        using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Premultiplied,
                (uint)writeableBitmap.PixelWidth,
                (uint)writeableBitmap.PixelHeight,
                96,
                96,
                pixels);
            await encoder.FlushAsync();

            using (var outputStream = writeStream.GetOutputStreamAt(0))
            {
                await outputStream.FlushAsync();
            }
        }
    }

BitmapEncoder.CreateAsync方法有一个overload,可添加一个参数来指定encoding options列表。编码选项参数的一个示例是

var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();

var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue(
    1.0, // Maximum quality
    Windows.Foundation.PropertyType.Single
    );

propertySet.Add("ImageQuality", qualityValue);
var encoder = await BitmapEncoder.CreateAsync(
    encoderId,
    writeStream
    propertySet);

然后您可以将SaveToJpg写为:

    /// <summary>
    /// Saves the WriteableBitmap to the given JPG file with the specified image quality.
    /// </summary>
    /// <param name="writeableBitmap">The writeable bitmap.</param>
    /// <param name="outputFile">The output file.</param>
    /// <param name="imageQuality">Valid values from 0 to 1.0. Higher values indicate higher quality.</param>
    /// <returns></returns>
    public static async Task SaveToJpg(
        WriteableBitmap writeableBitmap,
        StorageFile outputFile,
        double imageQuality = 1.0 // Maximum quality
    )
    {
        Stream stream = writeableBitmap.PixelBuffer.AsStream();
        byte[] pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);

        using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            var propertySet = new Windows.Graphics.Imaging.BitmapPropertySet();
            var qualityValue = new Windows.Graphics.Imaging.BitmapTypedValue(
                imageQuality,
                Windows.Foundation.PropertyType.Single
                );
            propertySet.Add("ImageQuality", qualityValue);
            var encoder = await BitmapEncoder.CreateAsync(
                BitmapEncoder.JpegEncoderId,
                writeStream
                propertySet);
            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Premultiplied,
                (uint)writeableBitmap.PixelWidth,
                (uint)writeableBitmap.PixelHeight,
                96,
                96,
                pixels);
            await encoder.FlushAsync();

            using (var outputStream = writeStream.GetOutputStreamAt(0))
            {
                await outputStream.FlushAsync();
            }
        }
    }