这是 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();
}
}
}