Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Uwp 将控件的屏幕截图保存到剪贴板_Uwp - Fatal编程技术网

Uwp 将控件的屏幕截图保存到剪贴板

Uwp 将控件的屏幕截图保存到剪贴板,uwp,Uwp,我正在尝试将UWP UIElement的屏幕截图(位图)复制到剪贴板,适合粘贴到例如Paint中 以下是我目前掌握的情况: public async void CopyScreenshot(UIElement content) { DataPackage dataPackage = new(); using (InMemoryRandomAccessStream stream = new())

我正在尝试将UWP UIElement的屏幕截图(位图)复制到剪贴板,适合粘贴到例如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();
        }
此代码将运行,但在尝试粘贴结果时,我会收到以下消息:

“剪贴板上的信息无法插入到绘图中”

当我使用RenderContentTorMasterStreamAsync方法将UIElement的图片保存到文件中时,该方法可以根据需要工作

要复制到剪贴板,我需要更改什么?提前谢谢

将控件的屏幕截图保存到剪贴板

在派生测试中,它看起来源于
RenderContentToRasterStreamAsync
的流在插入数据包之前已关闭。在官方的测试过程中,它可以复制和暂停图像进行喷漆。基于此线程,我编辑了
RenderContentToRasterStreamAsync1
thread,将流作为返回,然后将其作为位图加载。哈哈,很好吃

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;
}
public异步void CopyScreenshot1(UIElement内容)
{
DataPackage DataPackage=新数据包();
var stream=await RenderContentToRasterStreamAsync1(内容);
dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
尝试
{
剪贴板.SetContent(数据包);
}
捕获(例外)
{
}
}
专用异步任务RenderContentTorMasterStreamAsync1(UIElement内容)
{
RenderTargetBitmap RenderTargetBitmap=新建RenderTargetBitmap();
等待renderTargetBitmap.RenderAsync(内容);
var pixelBuffer=await renderTargetBitmap.GetPixelsAsync();
var pixels=pixelBuffer.ToArray();
var displayInformation=displayInformation.GetForCurrentView();
var stream=新的InMemoryRandomAccessStream();
var encoder=await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,stream);
编码器.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.预乘,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
像素);
等待编码器。FlushAsync();
回流;
}