如何在windows商店应用程序中进行屏幕截图

本文关键字:屏幕截图 应用程序 windows | 更新日期: 2023-09-27 18:00:09

我想在我的应用程序中截图,并用唯一的名称将其保存在应用程序的本地文件夹中。所以请帮帮我。

如何在windows商店应用程序中进行屏幕截图

您可以使用RenderTargetBitmap捕获屏幕。试试这个代码:

//create and capture Window
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Window.Current.Content);
//create unique file in LocalFolder
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("screenshotCapture.jpg", CreationCollisionOption.GenerateUniqueName);
//create JPEG image 
using (var stream = await file.OpenStreamForWriteAsync())
{
    var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream.AsRandomAccessStream());
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                            BitmapAlphaMode.Straight,
                            (uint)renderTargetBitmap.PixelWidth,
                            (uint)renderTargetBitmap.PixelHeight, logicalDpi, logicalDpi,
                            pixelBuffer.ToArray());
    await encoder.FlushAsync();
}

public static async Task<StorageFile> AsUIScreenShotFileAsync(this UIElement elememtName, string ReplaceLocalFileNameWithExtension)
    {
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(ReplaceLocalFileNameWithExtension, CreationCollisionOption.ReplaceExisting);
        try
        {
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
            // Render to an image at the current system scale and retrieve pixel contents 
            await renderTargetBitmap.RenderAsync(elememtName);
            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            // Encode image to an in-memory stream 
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight,
                DisplayInformation.GetForCurrentView().LogicalDpi,
                DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray());
            await encoder.FlushAsync();
            //CreatingFolder
           // var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromStream(stream);
            var streamWithContent = await rasr.OpenReadAsync();
            byte[] buffer = new byte[streamWithContent.Size];
            await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);

            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
                {
                    using (DataWriter dataWriter = new DataWriter(outputStream))
                    {
                        dataWriter.WriteBytes(buffer);
                        await dataWriter.StoreAsync(); // 
                        dataWriter.DetachStream();
                    }
                    // write data on the empty file:
                    await outputStream.FlushAsync();
                }
                await fileStream.FlushAsync();
            }
           // await file.CopyAsync(folder, "tempFile.jpg", NameCollisionOption.ReplaceExisting);
        }
        catch (Exception ex)
        {
            Reporting.DisplayMessageDebugExemption(ex.Message);
        }
        return file;
    }