![](/img/trans.png)
[英]is this possible to develop Call Blocker app in Windows Phone 8.0/8.1?
[英]Windows Phone 8.0 To Windows Phone 8.1 App
我正在寫一個Windows Phone 8.1(WINRT)應用程序 。 我有一個@ Sergio0694給出的代碼,它在Windows Phone 8.0 / Silverlight上運行良好。 它壓縮從圖庫中選取的用戶照片,然后轉換為base64字符串。
它不適用於Windows Phone 8.1。 誰能幫我 ?
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
並等待它時,除了使用TaskCompletionSource
設置結果TaskCompletionSource.SetResult
您還應該處理可能發生的任何異常,當您通過調用TaskCompletionSource.SetException
等待TaskCompletionSource.Task
時,這些異常不會被冒泡。
很可能當你處理一個大文件時拋出一個異常,因此你的await completionSource.Task
會因為沒有被調用SetResult
而掛起。
為了說明這一點,這里有一個代碼示例,該代碼執行您正在嘗試使用您的邏輯調度程序執行的操作,但也處理異常,如上所述:
public async Task<string> TestTaskCompletionSource(CoreDispatcher dispatcher)
{
TaskCompletionSource<bool> completionSource = new TaskCompletionSource<bool>();
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
try
{
Debug.WriteLine("Before delay");
await Task.Delay(100);
Debug.WriteLine("After delay and before exception");
throw new Exception("Test");
#pragma warning disable 162
completionSource.SetResult(true);
#pragma warning restore 162
}
catch (Exception e)
{
completionSource.SetException(e);
}
});
await completionSource.Task;
return "Test";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.