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>
目前還不清楚你所說的* 「多畫面」 *。它是gif嗎?或每個窗口的截圖?或者你想每次使用不同的文件名(然後添加'DateTime.Now.ToString()'到文件名)? – Sinatr
要獲得打印屏幕: http://stackoverflow.com/questions/35121443/how-to-print-a-screen-capture-in-wpf –
@Sinatr我不明白你的解決方案 請給我一個樣品項目 –