【UWP】截图

it2022-05-09  15

完整代码如下:

string desiredName =DateTime.Now.Ticks+".jpg";

            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

            StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

            StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

            RenderTargetBitmap bitmap = new RenderTargetBitmap();

            await bitmap.RenderAsync(PicGrid);

            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();

            }

            await new MessageDialog("保存成功").ShowAsync();

链接:https://www.jianshu.com/p/939c8fd4d75b

转载于:https://www.cnblogs.com/MiLu/p/11058929.html

相关资源:自己写的截图/截屏源代码

最新回复(0)