Fixing the screenshot functionality.

This commit is contained in:
Tadas Baltrusaitis
2018-09-18 19:51:37 +01:00
parent 630aba9006
commit e3545343ca

View File

@@ -195,7 +195,7 @@ namespace HeadPoseLive
InitializeComponent(); InitializeComponent();
DateTime now = DateTime.Now; DateTime now = DateTime.Now;
// Set the icon // Set the icon
Uri iconUri = new Uri("logo1.ico", UriKind.RelativeOrAbsolute); Uri iconUri = new Uri("logo1.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri); this.Icon = BitmapFrame.Create(iconUri);
@@ -722,23 +722,25 @@ namespace HeadPoseLive
PauseButton.Content = "Pause"; PauseButton.Content = "Pause";
} }
} }
private void ScreenshotButton_Click(object sender, RoutedEventArgs e) private void ScreenshotButton_Click(object sender, RoutedEventArgs e)
{ {
int Top = (int)this.Top; PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
int Left = (int)this.Left;
int Width = (int)this.Width; var topLeft = source.CompositionTarget.TransformToDevice.Transform(new System.Windows.Point(this.Left, this.Top));
int Height = (int)this.Height; var bottomRight = source.CompositionTarget.TransformToDevice.Transform(new System.Windows.Point(this.Left + this.Width, this.Top + this.Height));
int Width = (int)(bottomRight.X - topLeft.X);
int Height = (int)(bottomRight.Y - topLeft.Y);
using (Bitmap bmpScreenCapture = new Bitmap(Width, using (Bitmap bmpScreenCapture = new Bitmap(Width,
Height)) Height))
{ {
using (System.Drawing.Graphics g = Graphics.FromImage(bmpScreenCapture)) using (System.Drawing.Graphics g = Graphics.FromImage(bmpScreenCapture))
{ {
g.CopyFromScreen(Left, g.CopyFromScreen((int)(topLeft.X),
Top, (int)(topLeft.Y),
0, 0, 0, 0,
bmpScreenCapture.Size, bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy); CopyPixelOperation.SourceCopy);