2016-08-24 48 views
0

我想點擊簡單的屏幕簡短。如何在wpf中捕獲屏幕?

我從c#窗體創建一個簡單的項目。

但我不能在wpf上。

下面是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 
     } 

但它僅保存一張圖片。

問題的WPF:

1:我如何能捕捉屏幕上點擊?

2:如何一次保存多張照片?

注:

只是簡單的

請幫我人

+0

目前還不清楚你所說的* 「多畫面」 *。它是gif嗎?或每個窗口的截圖?或者你想每次使用不同的文件名(然後添加'DateTime.Now.ToString()'到文件名)? – Sinatr

+0

要獲得打印屏幕: http://stackoverflow.com/questions/35121443/how-to-print-a-screen-capture-in-wpf –

+0

@Sinatr我不明白你的解決方案 請給我一個樣品項目 –

回答

0

我用下面的代碼來創建一個屏幕截圖,並顯示在一個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)); 
+0

@unKerativ它將保存在哪裏? –

+0

嘿它是所有錯誤! https://i.imgur.com/ZznYksM.jpg –

1
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:

<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> 

+0

嘿 這是錯誤:> Format32bppArgb –