private System.Drawing.Bitmap GetScreenshot() { try { var rct = WindowSupplement.GetWindowRect(_mainWindow); var screenshot = new System.Drawing.Bitmap((int)rct.Width, (int)rct.Height); using (var g = System.Drawing.Graphics.FromImage(screenshot)) { g.CopyFromScreen(new System.Drawing.Point((int)rct.Left, (int)rct.Top), System.Drawing.Point.Empty, screenshot.Size); } return(screenshot); } catch (Exception ex) { MessageBox.Show("Failed to save screenshot. " + ex.Message, ProductInfo.Title, MessageBoxButton.OK, MessageBoxImage.Error); return(null); } }
/// <summary> /// Start reading and analyzing disk. /// </summary> private async Task RunAsync() { if (!Account.IsAdmin) { MessageBox.Show("This application needs to be run by administrator.", ProductInfo.Title, MessageBoxButton.OK, MessageBoxImage.Information); return; } if ((Settings.Current.Method == ReadMethod.Native) && !DiskReader.NativeExeExists) { MessageBox.Show("Win32 console application for native method is not found.", ProductInfo.Title, MessageBoxButton.OK, MessageBoxImage.Information); return; } // Prepare to store settings and data. if (!_diskScores[0].IsPinned) { _diskScores[0] = new DiskScore(); } else // If preceding score is pinned. { _diskScores.Insert(0, new DiskScore()); } _diskScores[0].Disk = CurrentDisk.Clone(); _diskScores[0].StartTime = DateTime.Now; _diskScores[0].BlockSize = Settings.Current.BlockSize; _diskScores[0].BlockOffset = Settings.Current.BlockOffset; _diskScores[0].AreaSize = Settings.Current.AreaSize; _diskScores[0].AreaLocation = Settings.Current.AreaLocation; _diskScores[0].AreaRatioInner = Settings.Current.AreaRatioInner; _diskScores[0].AreaRatioOuter = Settings.Current.AreaRatioOuter; _diskScores[0].NumRun = Settings.Current.NumRun; _diskScores[0].Method = Settings.Current.Method; // Reset scores and chart. UpdateScores(); _mainWindow.DrawChart(DrawMode.Clear); try { // Start. await Op.ReadAnalyzeAsync(new Progress <ProgressInfo>(UpdateProgress)); } catch (Exception ex) { Debug.WriteLine("Failed to read disk. {0}", ex); } // Save screenshot and log. if (Settings.Current.SavesScreenshotLog && !Op.IsCanceled) { // Wait for rendering of scores and chart. // (Synchronously start empty action of lower priority than rendering.) _mainWindow.Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null); WindowSupplement.ActivateWindow(_mainWindow); // Wait for this window to be activated (provided up to 10 times). for (int i = 0; i <= 9; i++) { if (WindowSupplement.IsWindowActivated(_mainWindow)) { break; } await Task.Delay(TimeSpan.FromMilliseconds(100)); } await SaveScreenshotLog(); } }