如何在 wpf 中截屏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39119704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:54:53  来源:igfitidea点击:

how to screen capture in wpf?

c#wpfscreenshot

提问by Tania Akter

I want to simple screen short on click.

我想在点击时简单地缩短屏幕。

I create a simple project from c# windows form.

我从 c# windows 窗体创建了一个简单的项目。

But I can not on wpf.

但是我不能上wpf。

Here is windows form code:

这是windows窗体代码:

using System.Drawing;
using System.Windows.Forms;

 private void short_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(1000);
            System.Windows.Forms.SendKeys.Send("{PRTSC}");
            Image img = Clipboard.GetImage();
            pictureBox1.Image = img;
            img.Save("D:\myimg.jpg");  //Its save only one picture 
        }

But it save only one picture .

但它只保存一张图片。

Question for wpf:

wpf的问题:

1:How can I capture screen on click?

1:如何在点击时截屏?

2:How can I save multiple picture at a time?

2:如何一次保存多张图片?

Note:

笔记:

Just simple

很简单

Please help me anybody

请帮助我任何人

Please

回答by Artyom Shmarlovsky

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr onj);

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        Bitmap bitmap;

        bitmap = new Bitmap((int) SystemParameters.PrimaryScreenWidth,(int) SystemParameters.PrimaryScreenHeight,PixelFormat.Format32bppArgb);

        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(0,0,0,0,bitmap.Size);
        }
        IntPtr handle = IntPtr.Zero;
        try
        {
            handle = bitmap.GetHbitmap();
            ImageControl.Source = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            bitmap.Save("D:\1.jpg"); //saving
        }
        catch (Exception)
        {

        }

        finally
        {
            DeleteObject(handle);
        }
    }
}

XAML:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>

    </Grid.RowDefinitions>
    <Button Grid.Row="1" Height="10" Click="ButtonBase_OnClick"></Button>
    <Image Grid.Row="0" x:Name="ImageControl"></Image>
</Grid>

回答by unkreativ

I use the following code to create a screenshot and show it on a wpf Image control. You should be able to save the bitmap as a jpeg or so

我使用以下代码创建屏幕截图并将其显示在 wpf Image 控件上。您应该能够将位图保存为 jpeg 左右

var bitmap = new Bitmap(Screen.AllScreens[SelectedScreen.Index].Bounds.Width, Screen.AllScreens[SelectedScreen.Index].Bounds.Height);


var graphics = Graphics.FromImage(bitmap);

graphics.CopyFromScreen(Screen.AllScreens[SelectedScreen.Index].Bounds.Left, Screen.AllScreens[SelectedScreen.Index].Bounds.Top, 0, 0, bitmap.Size);

this.Dispatcher.Invoke(() => this.PreviewImage.Source = this.ConvertBitmapToBitmapImage(bitmap));