1

I want to simple screen short on click.

I create a simple project from c# windows form.

But I can not on wpf.

Here is windows form code:

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:

1: How can I capture screen on click?

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

Note:

Just simple

Please help me anybody

Please

Tania Akter
  • 85
  • 1
  • 1
  • 10
  • It's not clear what you call *"multiple picture"*. Is it gif? Or screenshot of each window? Or you want to use different file name each time (then just add `DateTime.Now.ToString()` to file name)? – Sinatr Aug 24 '16 at 09:44
  • To get print screen: http://stackoverflow.com/questions/35121443/how-to-print-a-screen-capture-in-wpf – Mr.B Aug 24 '16 at 09:47
  • @Sinatr I am not understood your solution Please give me a sample project – Tania Akter Aug 24 '16 at 09:50
  • I can't because I don't understand your question either ;) – Sinatr Aug 24 '16 at 09:51
  • @Sinatr I want to save multiple screen short like windows default screen short – Tania Akter Aug 24 '16 at 09:51

2 Answers2

3
 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

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

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));
unkreativ
  • 482
  • 2
  • 8