我想点击简单的屏幕。
我从c#windows form创建一个简单的项目。
但是我不能用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 :(得分: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>
答案 1 :(得分:0)
我使用以下代码创建屏幕截图并在wpf图像控件上显示它。您应该能够将位图保存为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));