0

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

我用我需要的所有东西创建了一个图像,但是图像的质量真的很差。你知道我如何升级它吗?

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

    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);

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

4

1 回答 1

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方法有一个重载,它添加一个参数来指定一个编码选项列表。编码选项参数的一个示例是

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();
            }
        }
    }
于 2013-04-08T21:44:41.690 回答