将控件截图保存到剪贴板 [英] Save screenshot of control to clipboard

查看:23
本文介绍了将控件截图保存到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 UWP UIElement 的屏幕截图(位图)复制到剪贴板,适合粘贴到例如油漆.

I'm trying to copy a screenshot (bitmap) of a UWP UIElement to the clipboard, suitable for pasting into e.g. Paint.

这是我目前所拥有的:

        public async void CopyScreenshot(UIElement content)
        {
            DataPackage dataPackage = new();

            using (InMemoryRandomAccessStream stream = new())
            {
                await RenderContentToRasterStreamAsync(content, stream);
                stream.Seek(0);    
                
                dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));  
            }

            try
            {
                Clipboard.SetContent(dataPackage);
                Data.AppStatusText = "Screenshot copied to clipboard.";
            }
            catch (Exception)
            {
                Data.AppStatusText = "Copying to clipboard failed.";
            }
        }

        private async Task RenderContentToRasterStreamAsync(UIElement content, IRandomAccessStream stream)
        {
            RenderTargetBitmap renderTargetBitmap = new();
            await renderTargetBitmap.RenderAsync(content);

            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            var pixels = pixelBuffer.ToArray();
            var displayInformation = DisplayInformation.GetForCurrentView();

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Premultiplied,
                (uint)renderTargetBitmap.PixelWidth,
                (uint)renderTargetBitmap.PixelHeight,
                displayInformation.RawDpiX,
                displayInformation.RawDpiY,
                pixels);

            await encoder.FlushAsync();
        }

此代码运行,但在尝试粘贴结果时,我收到消息:

This code runs, but when attempting to paste the result I get the message:

剪贴板上的信息无法插入画图".

"The information on the clipboard cannot be inserted into Paint".

当我使用 RenderContentToRasterStreamAsync 方法将 UIElement 的图片保存到文件时,它可以正常工作.

The RenderContentToRasterStreamAsync method works as desired when I use it to save a picture of the UIElement to a file.

我需要更改什么才能复制到剪贴板?提前致谢.

What do I need to change to copy to the clipboard? Thanks in advance.

推荐答案

将控件截图保存到剪贴板

Save screenshot of control to clipboard

派生测试,看起来来自RenderContentToRasterStreamAsync的流在插入数据包之前已经关闭.在使用官方 代码示例进行测试期间,它可以为Paint复制和暂停图像.基于此线程,我编辑了 RenderContentToRasterStreamAsync1 线程并将流作为返回,然后将其加载为位图.哈哈,好用!

Derive testing, it looks the stream comes from RenderContentToRasterStreamAsync has been closed before insert into the data package. During testing with official code sample, it could copy and pause image for Paint. Base on this thread, I have edited RenderContentToRasterStreamAsync1 thread and make the stream as return, then load it as bitmap. haha, It woks!

public async void CopyScreenshot1(UIElement content)
{
    DataPackage dataPackage = new DataPackage();
    var stream = await RenderContentToRasterStreamAsync1(content);
    dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));

    try
    {
        Clipboard.SetContent(dataPackage);

    }
    catch (Exception)
    {

    }
}

private async Task<InMemoryRandomAccessStream> RenderContentToRasterStreamAsync1(UIElement content)
{
    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(content);

    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
    var pixels = pixelBuffer.ToArray();
    var displayInformation = DisplayInformation.GetForCurrentView();

    var stream = new InMemoryRandomAccessStream();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Premultiplied,
        (uint)renderTargetBitmap.PixelWidth,
        (uint)renderTargetBitmap.PixelHeight,
        displayInformation.RawDpiX,
        displayInformation.RawDpiY,
        pixels);

    await encoder.FlushAsync();

    return stream;
}

这篇关于将控件截图保存到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆