1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-02 02:34:08 +02:00

Add a debug flag to generate individual screenshot frames

This commit is contained in:
chylex 2018-04-28 18:15:25 +02:00
parent a9cce13eef
commit 7558551859
2 changed files with 70 additions and 6 deletions

View File

@ -58,16 +58,17 @@ private void SetScreenshotHeight(int height){
SetNotificationSize(width, height); SetNotificationSize(width, height);
} }
public void TakeScreenshot(){ public bool TakeScreenshot(){
if (ClientSize.Height == 0){ if (ClientSize.Height == 0){
FormMessage.Error("Screenshot Failed", "Could not detect screenshot size.", FormMessage.OK); FormMessage.Error("Screenshot Failed", "Could not detect screenshot size.", FormMessage.OK);
return; return false;
} }
IntPtr context = NativeMethods.GetDC(this.Handle); IntPtr context = NativeMethods.GetDC(this.Handle);
if (context == IntPtr.Zero){ if (context == IntPtr.Zero){
FormMessage.Error("Screenshot Failed", "Could not retrieve a graphics context handle for the notification window to take the screenshot.", FormMessage.OK); FormMessage.Error("Screenshot Failed", "Could not retrieve a graphics context handle for the notification window to take the screenshot.", FormMessage.OK);
return false;
} }
else{ else{
using(Bitmap bmp = new Bitmap(ClientSize.Width, ClientSize.Height, PixelFormat.Format32bppRgb)){ using(Bitmap bmp = new Bitmap(ClientSize.Width, ClientSize.Height, PixelFormat.Format32bppRgb)){
@ -78,6 +79,7 @@ public void TakeScreenshot(){
} }
Clipboard.SetImage(bmp); Clipboard.SetImage(bmp);
return true;
} }
} }
} }

View File

@ -1,17 +1,33 @@
// Uncomment to keep screenshot windows visible for debugging #if DEBUG
// Uncomment to keep screenshot windows visible for debugging
// #define NO_HIDE_SCREENSHOTS // #define NO_HIDE_SCREENSHOTS
// Uncomment to generate screenshots of individual frames for at most 1 second
#define GEN_SCREENSHOT_FRAMES
#endif
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using TweetDuck.Core.Controls; using TweetDuck.Core.Controls;
using TweetDuck.Plugins; using TweetDuck.Plugins;
#if GEN_SCREENSHOT_FRAMES
using System.Drawing.Imaging;
using System.IO;
using TweetDuck.Core.Utils;
#endif
namespace TweetDuck.Core.Notification.Screenshot{ namespace TweetDuck.Core.Notification.Screenshot{
sealed class TweetScreenshotManager : IDisposable{ sealed class TweetScreenshotManager : IDisposable{
private readonly FormBrowser owner; private readonly FormBrowser owner;
private readonly PluginManager plugins; private readonly PluginManager plugins;
private readonly Timer timeout; private readonly Timer timeout;
private readonly Timer disposer; private readonly Timer disposer;
#if GEN_SCREENSHOT_FRAMES
private readonly Timer debugger;
private int frameCounter;
#endif
private FormNotificationScreenshotable screenshot; private FormNotificationScreenshotable screenshot;
@ -24,6 +40,11 @@ public TweetScreenshotManager(FormBrowser owner, PluginManager pluginManager){
this.disposer = new Timer{ Interval = 1 }; this.disposer = new Timer{ Interval = 1 };
this.disposer.Tick += disposer_Tick; this.disposer.Tick += disposer_Tick;
#if GEN_SCREENSHOT_FRAMES
this.debugger = new Timer{ Interval = 16 };
this.debugger.Tick += debugger_Tick;
#endif
} }
private void timeout_Tick(object sender, EventArgs e){ private void timeout_Tick(object sender, EventArgs e){
@ -46,7 +67,11 @@ public void Trigger(string html, int width){
screenshot.Show(); screenshot.Show();
timeout.Start(); timeout.Start();
#if !(DEBUG && NO_HIDE_SCREENSHOTS) #if GEN_SCREENSHOT_FRAMES
StartDebugger();
#endif
#if !NO_HIDE_SCREENSHOTS
owner.IsWaiting = true; owner.IsWaiting = true;
#endif #endif
} }
@ -58,8 +83,8 @@ private void Callback(){
timeout.Stop(); timeout.Stop();
screenshot.TakeScreenshot(); screenshot.TakeScreenshot();
#if !(DEBUG && NO_HIDE_SCREENSHOTS) #if !NO_HIDE_SCREENSHOTS
OnFinished(); OnFinished();
#else #else
screenshot.MoveToVisibleLocation(); screenshot.MoveToVisibleLocation();
@ -68,15 +93,52 @@ private void Callback(){
} }
private void OnFinished(){ private void OnFinished(){
#if GEN_SCREENSHOT_FRAMES
debugger.Stop();
#endif
screenshot.Location = ControlExtensions.InvisibleLocation; screenshot.Location = ControlExtensions.InvisibleLocation;
owner.IsWaiting = false; owner.IsWaiting = false;
disposer.Start(); disposer.Start();
} }
public void Dispose(){ public void Dispose(){
#if GEN_SCREENSHOT_FRAMES
debugger.Dispose();
#endif
timeout.Dispose(); timeout.Dispose();
disposer.Dispose(); disposer.Dispose();
screenshot?.Dispose(); screenshot?.Dispose();
} }
#if GEN_SCREENSHOT_FRAMES
private static readonly string DebugScreenshotPath = Path.Combine(Program.StoragePath, "TD_Screenshots");
private void StartDebugger(){
frameCounter = 0;
try{
Directory.Delete(DebugScreenshotPath, true);
WindowsUtils.TrySleepUntil(() => !Directory.Exists(DebugScreenshotPath), 1000, 10);
}catch(DirectoryNotFoundException){}
Directory.CreateDirectory(DebugScreenshotPath);
debugger.Start();
}
private void debugger_Tick(object sender, EventArgs e){
if (frameCounter < 63 && screenshot.TakeScreenshot()){
try{
Clipboard.GetImage()?.Save(Path.Combine(DebugScreenshotPath, "frame_"+(++frameCounter)+".png"), ImageFormat.Png);
}catch{
System.Diagnostics.Debug.WriteLine("Failed generating frame "+frameCounter);
}
}
else{
debugger.Stop();
}
}
#endif
} }
} }