public async void CaptureGif() { if (gifCapturing) { return; } gifCapturing = true; GifOverlayNotification notification = new GifOverlayNotification() { Title = "GifOverlay" }; this.GifOverlayRequest.Raise(notification); if (notification != null && notification.Confirmed) { int x = notification.WindowLeft; int y = notification.WindowTop; int w = notification.WindowWidth; int h = notification.WindowHeight; int f = notification.GifFramerate; int d = notification.GifDuration; string datePattern = string.Empty != settings.dateTimeString ? settings.dateTimeString : defaultDateTimePattern; string path = GetImageSavePath(); string filename = FilenameExistsCheck(path, $"gif_{DateTime.Now.ToString(datePattern)}", ".gif"); //Gif gif = new Gif(f, d, w, h, x, y, datePattern); Gif gif = new Gif(f, d, w, h, x, y, path, filename); if (!notification.LoadCache) { await gif.StartCapture(); } else { gif.LoadFromCache(); } if (gif.Frames.Count > 0) { bool cancelled = false; if (settings.gifEditorEnabled) { GifEditorNotification gen = new GifEditorNotification() { Title = "Gif Editor", Gif = gif }; this.GifEditorRequest.Raise(gen); if (gen != null && !gen.Confirmed) { cancelled = true; } } if (!cancelled) { GifProgressNotification gpn = new GifProgressNotification() { Title = "Encoding Gif..", Gif = gif }; this.GifProgressRequest.Raise(gpn); if (gpn != null && gpn.Confirmed) { //var filename = gpn.Name; //if (filename != string.Empty) //{ var img = CreateXImage(filename, Path.Combine(path, filename)); if (settings.gifUpload) { AddToQueue(img); } else { AddXimageToList(img, "", ""); } //} } } } gif = null; } gifCapturing = false; }
//public Task<string> EncodeGif(GifProgressNotification gpn) public Task <bool> EncodeGif(GifProgressNotification gpn) { return(Task.Run(() => { //string date = DateTime.Now.ToString(datePattern); //int count = 1; //string gifname = $"gif_{date}.gif"; //while (File.Exists(Path.Combine(Properties.Settings.Default.filePath, gifname))) //{ // gifname = $"gif_{date}({count}).gif"; // count++; //} bool success = true; List <string> filePaths = new List <string>(); foreach (GifFrame frame in frames) { if (frame.Selected) { filePaths.Add(frame.Filepath); } } try { //using (var gif = File.OpenWrite(Path.Combine(Properties.Settings.Default.filePath, gifname))) using (var gif = File.OpenWrite(Path.Combine(this.filepath, this.filename))) { using (var encoder = new GifEncoder(gif)) { var quantizer = new WuQuantizer(); //var histogram = new Histogram(); for (int i = 0; i < filePaths.Count; i++) { if (gpn.Cancelled) { break; } //using (var image = Image.FromStream(new MemoryStream(File.ReadAllBytes(filePaths[i])))) using (var image = new Bitmap(Image.FromStream(new MemoryStream(File.ReadAllBytes(filePaths[i]))))) { //using (var quantImage = quantizer.QuantizeImage(image, 10, 70, histogram, 256)) //using (var quantImage = quantizer.QuantizeImage(new Bitmap(image))) using (var quantImage = quantizer.QuantizeImage(image)) { encoder.AddFrame(quantImage, 0, 0, new TimeSpan(0, 0, 0, 0, delay)); } } EncodingProgress = (int)(((i + 1.0) / filePaths.Count) * 100.0); } quantizer = null; filePaths.Clear(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } if (gpn.Cancelled) { File.Delete(Path.Combine(this.filepath, this.filename)); //gifname = string.Empty; success = false; } //return gifname; return success; })); }