C# 如何在Winforms中将Bing地图图像的屏幕截图复制到剪贴板?

C# 如何在Winforms中将Bing地图图像的屏幕截图复制到剪贴板?,c#,winforms,screenshot,bing-maps,C#,Winforms,Screenshot,Bing Maps,在我的winforms应用程序中,我希望允许用户将Bing地图组件的屏幕截图复制到剪贴板(至少) 我从中找到了下面的代码,它超越了这一点,但它不适合我编译 SaveScreenshot(this.userControl11.myMap, "MapScreenshot.png"); // code in menu item click handler private async void SaveScreenshot(FrameworkElement captureSourc

在我的winforms应用程序中,我希望允许用户将Bing地图组件的屏幕截图复制到剪贴板(至少)

我从中找到了下面的代码,它超越了这一点,但它不适合我编译

SaveScreenshot(this.userControl11.myMap, "MapScreenshot.png"); // code in menu item click handler

private async void SaveScreenshot(FrameworkElement captureSource, string suggestedName)
{
    //Create a FileSavePicker.
    var savePicker = new Windows.Storage.Pickers.FileSavePicker()
    {
        DefaultFileExtension = ".png",
        SuggestedFileName = suggestedName,
        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
    };

    savePicker.FileTypeChoices.Add(".png", new System.Collections.Generic.List<string> { ".png" });

    //Prompt the user to select a file.
    var saveFile = await savePicker.PickSaveFileAsync();

    //Verify the user selected a file.
    if (saveFile != null)
    {                
        using (var fileStream = await saveFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
        {
            //Capture the screenshot and save it to the file stream.
            await ScreenshotToStreamAsync(captureSource, fileStream);
        }
    }
}

private async Task ScreenshotToStreamAsync(FrameworkElement element, IRandomAccessStream stream)
{
    var renderTargetBitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(element);

    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

    var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;

    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetPixelData(
        BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Ignore,
        (uint)renderTargetBitmap.PixelWidth,
        (uint)renderTargetBitmap.PixelHeight,
        dpi,
        dpi,
        pixelBuffer.ToArray());

    await encoder.FlushAsync();
}
所以我发现,这表明我需要的“使用”是使用Windows.Storage.Streams

…但当我添加此项时,我被告知无法找到“Windows”命名空间:

1>C:\Users\bclay\source\repos\MyMaps\MyMaps\Form1.cs(8,7,8,14): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?)

我需要做什么/更改/添加什么才能让它在Winforms上工作?还是我的情况需要一种完全不同的方法?

有几种方法可以做到这一点

作为一个简单(但绝对不是完美)的选项,您可以使用将屏幕复制到图形对象:

var r = elementHost1.ClientRectangle;
using (var img = new Bitmap(r.Width, r.Height))
{
    using (var g = Graphics.FromImage(img))
    {
        var sr = elementHost1.RectangleToScreen(r);
        g.CopyFromScreen(sr.Location, System.Drawing.Point.Empty, sr.Size);
    }
    System.Windows.Forms.Clipboard.SetImage(img);
}
另一个选项是使用和将其导出到图像,如下所示:

public System.Drawing.Image DrawToImage(
    System.Windows.Controls.Control target)
{
    var rtb = new System.Windows.Media.Imaging.RenderTargetBitmap(
        (int)(target.ActualWidth), (int)(target.ActualHeight),
            96, 96, System.Windows.Media.PixelFormats.Pbgra32);
    rtb.Render(target);
    System.Windows.Media.Imaging.PngBitmapEncoder encoder =
        new System.Windows.Media.Imaging.PngBitmapEncoder();
    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(rtb));
    var ms = new System.IO.MemoryStream();
    encoder.Save(ms);
    return System.Drawing.Image.FromStream(ms);
}
然后可以将其设置到剪贴板中:

using (var image = DrawToImage(userControl11.myMap))
    System.Windows.Forms.Clipboard.SetImage(image);
或将其保存到文件:

using (var image = DrawToImage(userControl11.myMap))
    image.Save(@"c:\test\map.png", System.Drawing.Imaging.ImageFormat.Png);

UWP应用程序可以使用
Windows.Storage.Streams
命名空间。选择器也是如此。您可以使用
RenderTargetBitmap
渲染地图对象的DrawingVisual,向UserControl添加一个内部方法(可能对WPF使用Bing地图控件,但对WinForms没有),并返回位图源或将其转换为位图。顺便说一句,ElementHost有一个
DrawToBitmap
方法。也许它可以呈现内容(我从来没有在Bing地图上使用过它,所以我不知道它是否会做任何事情或任何好事)。是的,我正在使用WPF Bing地图控件。完美!我使用了第二个选项并将其粘贴到剪贴板中。谢谢
using (var image = DrawToImage(userControl11.myMap))
    image.Save(@"c:\test\map.png", System.Drawing.Imaging.ImageFormat.Png);