how to screen capture in wpf? [duplicate] - c#

This question already has an answer here:
Desktop screenshot in WPF
(1 answer)
Closed 6 years ago.
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

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>

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

Related

UWP: How do you make a Border a click through shield?

I have situation where I want an overlay control to block UI interactions on a Page for everything that is behind a border. I have tried setting Border.ManipulationMode to False. I have set IsTapEnabled, IsRightTapEnabled, IsDoubleTapEnabled, and IsHitTestVisible to False.
I also tried subscribing to the Tapped and PointerEntered events, and setting the args Handled property to true. After all of this I can still click on Buttons through the border, and invoke their commands. Below are a few screenshots for context:
Page with no overlay
Page now has what should be an overlay that blocks controls behind it
A button capturing PointerOver that shouldn't be
Here is the UserControl xaml that becomes the overaly on the Page:
<UserControl x:Class="PocMvvmToolkitApp.Dialogs.DialogShell"
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"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="overlayGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<!--dialogShield is the Border that I want to prevent click through on-->
<Border x:Name="dialogShield"
Background="#AAFFFFFF"
IsHitTestVisible="False"
ManipulationMode="None"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
IsDoubleTapEnabled="False"
IsHoldingEnabled="False"
IsRightTapEnabled="False"
IsTapEnabled="False"/>
<Border x:Name="dialogBorder"
BorderBrush="Black"
BorderThickness="1" />
</Grid>
Attempting to handle the events:
public DialogShell()
{
this.InitializeComponent();
this.allDialogs = new List<ExtendedContentDialog>();
this.visibleDialogs = new List<ExtendedContentDialog>();
////Doesn't work
this.dialogShield.PointerEntered += this.OnModalShieldPointerEntered;
this.dialogShield.Tapped += this.OnModalShieldTapped;
}
private void OnModalShieldTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
////Doesn't block click through
e.Handled = true;
}
private void OnModalShieldPointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
e.Handled = true;
}
On the Page.xaml.cs here is where I add or remove the DialogShell control to the parent Grid on the page:
private void OnDialogStackChanged(Args.DialogStackChangedEventArgs args)
{
switch (args.Context)
{
case Args.DialogStackChangedContext.Showing:
if (this.dialogShell == null)
{
this.dialogShell = new DialogShell();
this.dialogShell.ShowDialog(args.Dialog);
this.rootGrid.Children.Add(this.dialogShell);
Grid.SetColumnSpan(this.dialogShell, 2);
}
break;
case Args.DialogStackChangedContext.Closing:
if (this.dialogShell != null)
{
this.dialogShell.RemoveDialog(args.Dialog);
if (this.dialogShell.AllDialogs.Count == 0)
{
this.rootGrid.Children.Remove(this.dialogShell);
this.dialogShell = null;
}
}
break;
}
}
Any help with this Border situation would be appreciated. Before someone recommends using ContentDialog, please don't, I have my reasons for this setup. Thanks!

Drawing image into a Window and update it

I have an unsafe class that generate a Bitmap which is converted to ToImageSource in order to draw into a Window. The Bitmap itself contains a sinusoidal text which is frequently updated and I want to it "move" from the left to the right (marquee style?). Anyway it works just fine in a WinForm but I'm stuck with the WPF Window.
Here are some code samples:
public AboutWindow()
{
InheritanceBehavior = InheritanceBehavior.SkipAllNow;
InitializeComponent();
Initialize();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawImage(bitmapRender.WindowBitmap, drawingArea);
if (!worker.IsBusy)
worker.RunWorkerAsync(); // BackgroundWorker in charge of updating the bitmap
}
void DispatcherTimerRender_Tick(object sender, EventArgs e) => InvalidateVisual();
My issues are: there is nothing displayed on the Window and the DispatchedTimer that calls InvalidateVisual() leads to this exception:
System.InvalidOperationException: 'Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.'
I have looked at other threads and I'm aware that WPF is a retained drawing system but I would love to achieve it anyway.
Any suggestion about the "best" way to achieve this?
Any useful explanation/link would be very much appreciated.
[Edit]
<Window x:Class="CustomerManagement.View.AboutWindow"
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"
mc:Ignorable="d" Height="450" WindowStartupLocation="CenterScreen" Width="800" ResizeMode="NoResize" AllowsTransparency="True" WindowStyle="None">
<Grid KeyDown="Grid_KeyDown">
<Image Width="800" Height="450" Source="{Binding 'Image'}" />
</Grid>
</Window>
You should use an Image element that has its Source property bound to an ImageSource property in a view model. This is the "standard" way, based on the MVVM architectural pattern, and therefore the "best" way - in my opinion.
<Image Source="{Binding Image}"/>
The view model could look like this:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ImageSource image;
public ImageSource Image
{
get { return image; }
set
{
image = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Image)));
}
}
}
and an instance of it would be assigned to the DataContext of the window:
public AboutWindow()
{
InitializeComponent();
var vm = new ViewModel();
DataContext = vm;
}
For testing it, the code below performs a slide show of image files in a directory. You may as well assign any other ImageSource - e.g. a DrawingImage - to the Image property.
var imageFiles = Directory.GetFiles(..., "*.jpg");
var index = -1;
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
timer.Tick += (s, e) =>
{
if (++index < imageFiles.Length)
{
vm.Image = new BitmapImage(new Uri(imageFiles[index]));
}
else
{
timer.Stop();
}
};
timer.Start();

Printing a WPF Visual Containing Images

I am trying to print a WPF visual (a Canvas to be exact) containing multiple images. The image sources are loaded from base64 strings that are converted to BitmapSources. When the canvas is shown in a Window the 2 images are displayed correctly, however when its printed using PrintVisual method of PrintDialog, the images both appear the same. I have created a scaled down example that exhibits the behavior that I am seeing.
Here is the XAML I'm using:
<Window x:Class="ImagePrintTest.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:ImagePrintTest"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="600">
<StackPanel>
<Button Content="Print" Click="Button_Click" />
<Canvas x:Name="ImageCanvas">
<Image x:Name="ImageA" Canvas.Left="50" Canvas.Top="50" Width="380" Height="56" />
<Image x:Name="ImageB" Canvas.Left="50" Canvas.Top="150" Width="380" Height="56" />
</Canvas>
</StackPanel>
And this is the code behind:
public partial class MainWindow : Window
{
string ABC_DATA = #"iVBORw0KGgoAAAANSUhEUgAAAXwAAAA4CAYAAAD+WUMEAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAxCSURBVHhe7d15cBPXHQfw7+q+JcuSbfm2aWywwRwmlHhCIAdtSpq0SSkz7aQJaabJH50m7TTptE0n+Seh02lnaKczaZv0SNp0kskxjXMMJENJORuOYBtsA8YHsi1fknVZklfn61tLBBsMCPA/ZX8fzxu0u5JWfzy++/a93bcCYwyEEEJufIrcv4QQQm5wFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITNB8+mUOMnWE9kVGcFuN8SWoPOLDYUYNqg0Uwz7wjD+kwYtMDbFfQh3gmk1s5P6VCD7OpCatNRhSolEJ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Xup7Ce0QjpqYaDTwZfyNl7qMzEQCb4Kq3HBxT7B+YA518fYwlrvYcVS8PUdV+vgy9KXb/giogfj1AphU2dqhUzcRC5qot/G2lMTVzxMlXyMFHRtCMeyFPq7Y3iqx8H5HgvDaQODcaUc2XTNSmLF1Ao35PAQl9fvjhrYGVFo+F4t+VSxN2vj5SEb+2Jq9Tw3daZWOD6UwpmhOF7MArtbmUaCaAtj4IfNnItPJkv6uyNl/GXax7w0Nb4HKKQiJnZ0muiTVO51ZMlFcTxn4ctZQ607j/2yjU6PvCwewdF8FBcOOziWNNC18hJQBj7R/+CQTthIUFtSTQ+YCnn5sbkdwEA+5eDccAo/eCKNX30upX64x1Ev5AwlxTkRtTlW+GHjeVJge3h3wtPXixr3pKmZCr8zZmJft43tETlgtHqpfdXDRMHT8h5wu6Ixt9TcNMtET8LEiS55tJVaNYuD56PoVvHupKdHCxoz0tSTkfeetvGIbJzhrQIUMgx8IqKQ4AdxIqKQYOATEYUEA5+IKCQY+EREIcHAJyIKCQY+EVFIMPCJiEKCgU9EFArAfwE2JuRnUGyVRgAAAABJRU5ErkJggg==";
public MainWindow()
{
InitializeComponent();
ImageA.Source = Convert(ABC_DATA);
ImageB.Source = Convert(DEF_DATA);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(ImageCanvas, "image print test");
}
}
public static BitmapSource Convert(string s)
{
byte[] bytes;
bytes = System.Convert.FromBase64String(s);
BitmapSource source;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
{
source = PngBitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
}
return source;
}
}
I am assuming that the image sources are being cached somehow somewhere but I really cant explain this behavior. Can anyone explain what is happening here?
It works for me when I create a second BitmapFrame like this:
public static BitmapSource Convert(string s)
{
using (var ms = new MemoryStream(System.Convert.FromBase64String(s)))
{
return BitmapFrame.Create(
BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad));
}
}
However, I don't have an explanation for this behaviour.
I also noticed that printing seems to spoil the layout. After printing, the ImageCanvas moves to position (0,0) in its StackPanel parent.

WPF : Move control c# code to change position from location to another

how can move (change position) label from position to another using wpf **
animation
** in specify event such click using c# code not trigger ??
using System.windows.media.animation;
XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click" Canvas.Left="201" Canvas.Top="116" x:Name="Button"/>
</Canvas>
</Window>
Code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var storyboard = new Storyboard();
var pointAnimation = new DoubleAnimation()
{
From = Canvas.GetLeft(Button),
To = Canvas.GetLeft(Button) + 200,
Duration = new Duration(TimeSpan.FromSeconds(10))
};
Storyboard.SetTarget(pointAnimation, Button);
Storyboard.SetTargetProperty(pointAnimation, new PropertyPath(Canvas.LeftProperty));
storyboard.Children.Add(pointAnimation);
storyboard.Begin();
}

How to display a pop-up?

I created a pop-up and want to show it when a button has been clicked. But it doesn't work because it waits for finishing button's duty.
I don't want to use "timer, thread and background worker" because I already can do it with that. I am looking for an another solution…
Here is my code:
void btnSearch_Click(object sender, RoutedEventArgs e)
{
LoadingOpen();
if (cmbvideoserver.Text == "Youtube")
SearchInYoutube();
else if (cmbvideoserver.Text == "Vimeo")
SearchInVimeo();
LoadingClose();
}
public void LoadingOpen()
{
myPopup.IsOpen = true;
Common.Popup = true;
window.Opacity = 0.3;
}
public void LoadingClose()
{
myPopup.IsOpen = false;
Common.Popup = false;
window.Opacity = 1;
}
and the XAML:
<Window Name="window" x:Class="youtube.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="window_Loaded">
<Canvas Name="main">
<Popup Name="myPopup" IsOpen="False" PopupAnimation="Slide"
Margin="100,10,20,0" Placement="Center" >
<Label Content="Loading..."></Label>
</Popup>
</Canvas>
</Window>
How can I show popup on my main window ? (without any thread,timer and background controls/classes)
You can show Windows as dialogs by useing the ShowDialog() method.

Categories

Resources