1

我无法将锁定屏幕设置为图像,但我认为这不是 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 类结束

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

4

0 回答 0