尝试在点击图像时以编程方式获取uwp中的屏幕截图

时间:2016-12-07 11:56:14

标签: c# xaml uwp

尝试捕获uwp上的截图但是出现了一些问题,说

1)可写地图不包含渲染的定义。

2)可写地图不包含Save.jpeg的定义

3)找不到类型或命名空间媒体库。 任何类型的帮助将不胜感激。 代码

        <plugin>
            <groupId>com.github.siom79.japicmp</groupId>
            <artifactId>japicmp-maven-plugin</artifactId>
            <version>0.9.3</version>
            <configuration>
                <oldVersion>
                    <dependency>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${lastBaseVersion}</version>
                        <type>jar</type>
                    </dependency>
                </oldVersion>
                <newVersion>
                    <file>
                        <path>${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}</path>
                    </file>
                </newVersion>
                <parameter>
                    <onlyModified>true</onlyModified>
                    <breakBuildBasedOnSemanticVersioning>true</breakBuildBasedOnSemanticVersioning>
                </parameter>
                <skip></skip>
            </configuration>
            <executions>
                <execution>
                    <phase>verify</phase>
                    <goals>
                        <goal>cmp</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

1 个答案:

答案 0 :(得分:1)

而不是WriteableBitmap使用RenderTargetBitmap来创建可见UIElement的位图表示。要将此位图另存为文件,您可以使用我创建的此扩展方法(here's a great example for extension methods):

public static class RenderTargetBitmapExtensions {
    public static async Task<StorageFile> ToFile(this RenderTargetBitmap renderTargetBitmap, string filename, StorageFolder folder = null, bool overrideExisting = true) {
        if (folder == null) folder = ApplicationData.Current.TemporaryFolder;

        try {
            byte[] pixels = (await renderTargetBitmap.GetPixelsAsync()).ToArray();

            StorageFile outputFile = await folder.CreateFileAsync(filename, overrideExisting ? CreationCollisionOption.ReplaceExisting : CreationCollisionOption.GenerateUnique);
            var bitmapEncodingMode = BitmapEncoder.PngEncoderId;

            using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) {
                var encoder = await BitmapEncoder.CreateAsync(bitmapEncodingMode, writeStream);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, 96, 96, pixels);
                await encoder.FlushAsync();
            }
            return outputFile;
        } catch {
            return null;
        }
    }
}

最后获取公共Images文件夹(因为MediaLibrary是Silverlight类,并且在UWP中不再存在)您可以根据this thread执行以下操作:

StorageFolder picturesLibrary = KnownFolders.PicturesLibrary;
StorageFolder savedPicturesFolder = await picturesLibrary.CreateFolderAsync("Saved Pictures", CreationCollisionOption.OpenIfExists);

注意:默认情况下,应用无法访问图片库。您必须在Package.appxmanifest中添加该功能。打开Package.appxmanifest并单击Capabilities选项卡。图片库有一个复选框。

所以这样做的整个代码应该是:

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(LayoutRoot, Convert.ToInt32(LayoutRoot.ActualWidth), Convert.ToInt32(LayoutRoot.ActualHeight));

StorageFolder savedPicturesFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Saved Pictures", CreationCollisionOption.OpenIfExists);

await renderTargetBitmap.ToFile("filename.jpg", savedPicturesFolder);

或者如果您不想覆盖现有文件,最后一行将是:

await renderTargetBitmap.ToFile("filename.jpg", savedPicturesFolder, false);

为此您可以创建基于时间的文件名:

string filename = String.Format("downloaded_{0}.jpg", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
await renderTargetBitmap.ToFile(filename, savedPicturesFolder, false);