Beispiel #1
0
        public static async Task <byte[]> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode)
        {
            if (!filePath.EndsWith(".lnk", StringComparison.Ordinal) && !filePath.EndsWith(".url", StringComparison.Ordinal))
            {
                var item = await StorageHelpers.ToStorageItem <IStorageItem>(filePath);

                if (item != null)
                {
                    var iconData = await LoadIconFromStorageItemAsync(item, thumbnailSize, thumbnailMode);

                    if (iconData != null)
                    {
                        return(iconData);
                    }
                }
            }
            return(await LoadIconWithoutOverlayAsync(filePath, thumbnailSize));
        }
Beispiel #2
0
        public static async void SetAsBackground(WallpaperType type, string filePath)
        {
            if (UserProfilePersonalizationSettings.IsSupported())
            {
                // Get the path of the selected file
                BaseStorageFile sourceFile = await StorageHelpers.ToStorageItem <BaseStorageFile>(filePath);

                if (sourceFile == null)
                {
                    return;
                }

                // Get the app's local folder to use as the destination folder.
                BaseStorageFolder localFolder = ApplicationData.Current.LocalFolder;

                // the file to the destination folder.
                // Generate unique name if the file already exists.
                // If the file you are trying to set as the wallpaper has the same name as the current wallpaper,
                // the system will ignore the request and no-op the operation
                BaseStorageFile file = await FilesystemTasks.Wrap(() => sourceFile.CopyAsync(localFolder, sourceFile.Name, NameCollisionOption.GenerateUniqueName).AsTask());

                if (file == null)
                {
                    return;
                }

                UserProfilePersonalizationSettings profileSettings = UserProfilePersonalizationSettings.Current;
                if (type == WallpaperType.Desktop)
                {
                    // Set the desktop background
                    await profileSettings.TrySetWallpaperImageAsync(await file.ToStorageFileAsync());
                }
                else if (type == WallpaperType.LockScreen)
                {
                    // Set the lockscreen background
                    await profileSettings.TrySetLockScreenImageAsync(await file.ToStorageFileAsync());
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Rotates the image at the specified file path.
        /// </summary>
        /// <param name="filePath">The file path to the image.</param>
        /// <param name="rotation">The rotation direction.</param>
        /// <remarks>
        /// https://docs.microsoft.com/en-us/uwp/api/windows.graphics.imaging.bitmapdecoder?view=winrt-22000
        /// https://docs.microsoft.com/en-us/uwp/api/windows.graphics.imaging.bitmapencoder?view=winrt-22000
        /// </remarks>
        public static async Task Rotate(string filePath, BitmapRotation rotation)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }

            var file = await StorageHelpers.ToStorageItem <IStorageFile>(filePath);

            if (file == null)
            {
                return;
            }

            var fileStreamRes = await FilesystemTasks.Wrap(() => file.OpenAsync(FileAccessMode.ReadWrite).AsTask());

            using IRandomAccessStream fileStream = fileStreamRes.Result;
            if (fileStream == null)
            {
                return;
            }

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

            using var memStream = new InMemoryRandomAccessStream();
            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);

            encoder.BitmapTransform.Rotation = rotation;

            await encoder.FlushAsync();

            memStream.Seek(0);
            fileStream.Seek(0);
            fileStream.Size = 0;

            await RandomAccessStream.CopyAsync(memStream, fileStream);
        }