public WebScreenshotService(WebScreenshotServiceSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings), $"{nameof(settings)} cannot be null."); } this.Settings = settings; this.Settings.EnsureUri(); this.Settings.EnsureBrowserBounds(); this._tscBrowserInitialized = new TaskCompletionSource <bool>(); this._tscLoadingStateChanged = new TaskCompletionSource <bool>(); }
protected virtual void Dispose(bool disposing) { if (!this._disposed) { if (disposing) { this.Settings = null; this._tscBrowserInitialized = null; this._tscLoadingStateChanged = null; } } this._disposed = true; }
private void TakeScreenshotAndGenerateThumbnailInternal(string url, string localPath) { var thumbnailFile = new FileInfo(localPath); var webScreenshotServiceSettings = new WebScreenshotServiceSettings { Uri = new Uri(url), BrowserWidth = this._settings.BrowserWidth, BrowserHeight = this._settings.BrowserHeight, ThumbnailWidth = this._settings.ThumbnailWidth, ThumbnailHeight = this._settings.ThumbnailHeight }; using (var webScreenshotService = new WebScreenshotService(webScreenshotServiceSettings)) using (var thumbnailTask = webScreenshotService.TakeScreenshotAndGenerateThumbnailAsync()) { var completed = thumbnailTask.Wait((int)this._settings.MillisecondsTimeoutWaitTask.TotalMilliseconds); if (!completed) { throw new Exception($"The thumbnail could not be generated. The process has waited for {this._settings.MillisecondsTimeoutWaitTask.TotalSeconds} seconds. Url: {url}."); } var exception = thumbnailTask.Exception; if (exception != null) { throw new Exception($"An internal error occurred while running the task. Url: {url}.", exception); } if (!thumbnailFile.Directory.Exists) { thumbnailFile.Directory.Create(); thumbnailFile.Directory.Refresh(); } if (thumbnailFile.Exists) { thumbnailFile.Delete(); thumbnailFile.Refresh(); } thumbnailTask.Result.Save(thumbnailFile.FullName, ImageFormat.Jpeg); } thumbnailFile.Refresh(); using (var image = new MagickImage(thumbnailFile.FullName)) { var isWhite = this.CheckImageColor(image, MagickColors.White); if (isWhite) { throw new Exception($"Something went wrong, the thumbnail is totally white. Url: {url}."); } var isBlack = this.CheckImageColor(image, MagickColors.Black); if (isBlack) { throw new Exception($"Something went wrong, the thumbnail is totally black. Url: {url}."); } } }