你可以用我的方法压缩图片,然后转成Base64字符串。
注意:我正在使用 WriteableBitmapEx 扩展类来调整图像大小。此外,WriteableBitmap 类需要在 UI 线程上实例化,因此如果您像我一样在后台任务上运行此方法,您必须将引用传递给您所在的页面,以便该方法当它使用 WriteableBitmap 类时,将能够让当前的 UI 调度程序工作。
这里是:
public static async Task<String> ToCompressedBase64(this StorageFile imageFile, Page localPage)
{
//Get the stream from the StorageFile
IRandomAccessStream imageStream = await imageFile.OpenAsync(FileAccessMode.Read);
System.Diagnostics.Debug.WriteLine("Original size ---> " + imageStream.ToFileSize());
//Compresses the image if it exceedes the maximum file size
imageStream.Seek(0);
BitmapDecoder compressDecoder = await BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider compressionData = await compressDecoder.GetPixelDataAsync();
byte[] compressionBytes = compressionData.DetachPixelData();
//Set target compression quality
BitmapPropertySet propertySet = new BitmapPropertySet();
BitmapTypedValue qualityValue = new BitmapTypedValue(0.5, PropertyType.Single);
propertySet.Add("ImageQuality", qualityValue);
imageStream.Seek(0);
imageStream = new InMemoryRandomAccessStream();
BitmapEncoder compressionEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, imageStream, propertySet);
compressionEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
compressDecoder.PixelWidth, compressDecoder.PixelHeight,
compressDecoder.DpiX, compressDecoder.DpiY, compressionBytes);
await compressionEncoder.FlushAsync();
//Create a BitmapDecoder from the stream
BitmapDecoder resizeDecoder = await BitmapDecoder.CreateAsync(imageStream);
#if DEBUG
System.Diagnostics.Debug.WriteLine("Old height and width ---> " + resizeDecoder.PixelHeight + " * " + resizeDecoder.PixelWidth + "\nCompressed size ---> " + imageStream.ToFileSize());
#endif
//Resize the image if needed
TaskCompletionSource<bool> completionSource = new TaskCompletionSource<bool>();
localPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
const int maxImageWidth = 48;
if (resizeDecoder.PixelWidth > maxImageWidth)
{
//Resize the image if it exceedes the maximum width
int newHeight = (int)(maxImageWidth * resizeDecoder.PixelHeight / resizeDecoder.PixelWidth);
WriteableBitmap tempBitmap = new WriteableBitmap((int)resizeDecoder.PixelWidth, (int)resizeDecoder.PixelHeight);
imageStream.Seek(0);
await tempBitmap.SetSourceAsync(imageStream);
WriteableBitmap resizedImage = tempBitmap.Resize(maxImageWidth, newHeight, WriteableBitmapExtensions.Interpolation.Bilinear);
//Assign to imageStream the resized WriteableBitmap
InMemoryRandomAccessStream resizedStream = new InMemoryRandomAccessStream();
await resizedImage.ToStream(resizedStream, BitmapEncoder.JpegEncoderId);
imageStream = resizedStream;
}
completionSource.SetResult(true);
}).Forget();
await completionSource.Task;
//Converts the final image into a Base64 String
imageStream.Seek(0);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider pixels = await decoder.GetPixelDataAsync();
#if DEBUG
System.Diagnostics.Debug.WriteLine("New height and width ---> " + decoder.PixelHeight + " * " + decoder.PixelWidth + "\nSize after resize ---> " + imageStream.ToFileSize());
#endif
byte[] bytes = pixels.DetachPixelData();
//Encode image
InMemoryRandomAccessStream encoded = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, encoded);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, bytes);
await encoder.FlushAsync();
encoded.Seek(0);
//Read bytes
byte[] outBytes = new byte[encoded.Size];
await encoded.AsStream().ReadAsync(outBytes, 0, outBytes.Length);
//Create Base64
return Convert.ToBase64String(outBytes);
}
注意:我使用的是 TaskCompletionSource 而不是在页面调度程序上等待 RunAsync 调用,因为如果您使用异步操作调用它,您实际上无法等待它,因为它返回一个任务给调用者,该方法将其返回而不等待操作完成。
所以我最终得到了内部异步操作,该操作在控件回到主方法后仍在运行。
使用该 TaskCompletionSource 并等待它可以解决问题:)
Forget 方法只是一种方法,当您在异步方法中调用异步方法而不等待它时,我必须抑制 VS 警告。
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Forget(this IAsyncAction action) { }
你可以这样称呼它:
Task<String> base64ResizedImage = Task.Run(async () => await args.Files[0].ToCompressedBase64(this));
另外,如果图像的宽度超过给定值,我的方法会调整图像的大小(我设置了 48,正如你在问题中所说的那样)。
如果您还想检查高度,只需编辑该部分并检查两个图像边界:)