UWP 实现保存image控件图片到本地

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csm201314/article/details/79765229

实现代码如下:

        private async void save_image(string desiredName) {
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            StorageFolder folder = await applicationFolder.CreateFolderAsync("Picture", CreationCollisionOption.OpenIfExists);
            StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);
            RenderTargetBitmap bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(this.Image);
            var pixelBuffer = await bitmap.GetPixelsAsync();
            using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                    BitmapAlphaMode.Ignore,
                                    (uint)bitmap.PixelWidth,
                                    (uint)bitmap.PixelHeight,
                                    DisplayInformation.GetForCurrentView().LogicalDpi,
                                    DisplayInformation.GetForCurrentView().LogicalDpi,
                                    pixelBuffer.ToArray());
                await encoder.FlushAsync();
            }
            this.Image.Source = new BitmapImage(new Uri(folder.Path + "/" + desiredName));
        }

猜你喜欢

转载自blog.csdn.net/csm201314/article/details/79765229
UWP
今日推荐