尽管工作Uri,LockScreen.SetImageUri ArgumentException

时间:2013-01-27 02:10:34

标签: c# windows-phone-8

我无法将锁定屏幕设置为图像,但我认为这不是Uri的问题,而是图像本身的问题。

当我尝试将图像设置为具有大致适合锁定屏幕尺寸的下载图像时,即使图像不是太大也是如此。但是当我尝试将锁屏图像设置为来自相机的照片(诺基亚Lumia 920)时,它给了我这个参数例外。

我的代码:

//来自StorageHelper类:

public async Task<bool> SavePhotoAsync(Photo photo, WriteableBitmap source, int height, int width)
    {
        return await Task.Run(() =>
            {
                try
                {
                    lock (StorageLock)
                    {
                        ObservableCollection<Photo> observableCollection = photo.PhotoAlbum.Photos as ObservableCollection<Photo>;
                        if (observableCollection == null)
                        {
                            return false;
                        }

                        if (!IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(photo.PhotoAlbum.Name))
                        {
                            IsolatedStorageFile.GetUserStoreForApplication().CreateDirectory(photo.PhotoAlbum.Name);
                        }

                        string fileName = photo.PhotoAlbum.Name + "/" + observableCollection.IndexOf(photo).ToString(CultureInfo.InvariantCulture);
                        if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(fileName))
                        {
                            IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(fileName);
                        }

                        IsolatedStorageFileStream fileStream =
                            IsolatedStorageFile.GetUserStoreForApplication().CreateFile(fileName);

                        int targetWidth = 0;
                        int targetHeight = 0;
                        if (source.PixelWidth < width || source.PixelHeight < height)
                        {
                            targetWidth = source.PixelWidth;
                            targetHeight = source.PixelHeight;
                        }
                        else
                        {
                            if (source.PixelWidth > source.PixelHeight)
                            {
                                double percentage = ((double)height)/((double) source.PixelHeight);
                                targetHeight = height;
                                targetWidth = (int) (source.PixelWidth * percentage);
                            }
                            else
                            {
                                double percentage = ((double)width) / ((double)source.PixelWidth);
                                targetWidth = width;
                                targetHeight = (int)(source.PixelHeight * percentage);
                            }
                        }

                        source.SaveJpeg(fileStream, targetWidth, targetHeight, 0, 100);

                        fileStream.Close();
                        return true;
                    }
                }
                catch (Exception e)
                {
                    return false;
                }

            });
    }

//结束StorageHelper

//来自我的LockscreenManager类:

public static async void SetLockScreenImages()
    {
        LockScreenRequestResult result = await LockScreenManager.RequestAccessAsync();
        if (result == LockScreenRequestResult.Granted)
        {
            Uri uri = null;
            try
            {
                uri = LockScreen.GetImageUri();
            }
            catch (Exception)
            {

            }

            List<Uri> uriList = await BuildUriList();
            if (uriList.Count == 0)
            {
                return;
            }

            int index = 0;
            if (uri != null && uriList.Any(uriEntry => uri.ToString() == uriEntry.ToString()))
            {
                index = (uriList.IndexOf(uri) + 1) % uriList.Count;
            }

            if (InterfaceProvider.GetIStorageService().FileExists(uriList[index]))
            {
                try
                {
                    LockScreen.SetImageUri(uriList[index]);
                }
                catch (Exception)
                {

                }

            }
        }
    }

// Lockscreenmanager类的结束

您可以忽略我尝试缩小图像,它没有帮助(但图像仍然正确保存)。我该怎么办?

0 个答案:

没有答案