4

I want to take the screenshot in my application and want to save it in local folder of app with unique name. so please help me.

vikas sharawat
  • 207
  • 1
  • 12
  • Did you even *try anything*? One second of ggogling and we found a duplicate: http://stackoverflow.com/questions/5049122/capture-the-screen-shot-using-net – HimBromBeere Aug 05 '16 at 06:56
  • 1
    @himbrombeere uwp and dotnet are not the same at all. – T.S Jul 20 '17 at 06:54

2 Answers2

6

You can capture you screen using RenderTargetBitmap. Try this code:

//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();
}
Andrii Krupka
  • 4,167
  • 3
  • 16
  • 39
0

Or

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;

    }
Amz
  • 51
  • 2