1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-18 15:15:48 +02:00

Fix screenshots with zoom & try to fix rendering issues

This commit is contained in:
chylex 2018-04-28 20:36:49 +02:00
parent 7558551859
commit 12525ac386
3 changed files with 30 additions and 14 deletions

View File

@ -15,11 +15,10 @@ sealed class FormNotificationScreenshotable : FormNotificationBase{
protected override bool CanDragWindow => false; protected override bool CanDragWindow => false;
private readonly PluginManager plugins; private readonly PluginManager plugins;
private readonly int width; private int height;
public FormNotificationScreenshotable(Action callback, FormBrowser owner, PluginManager pluginManager, string html, int width) : base(owner, false){ public FormNotificationScreenshotable(Action callback, FormBrowser owner, PluginManager pluginManager, string html, int width) : base(owner, false){
this.plugins = pluginManager; this.plugins = pluginManager;
this.width = width;
browser.RegisterAsyncJsObject("$TD_NotificationScreenshot", new ScreenshotBridge(this, SetScreenshotHeight, callback)); browser.RegisterAsyncJsObject("$TD_NotificationScreenshot", new ScreenshotBridge(this, SetScreenshotHeight, callback));
@ -36,11 +35,11 @@ public FormNotificationScreenshotable(Action callback, FormBrowser owner, Plugin
} }
using(IFrame frame = args.Browser.MainFrame){ using(IFrame frame = args.Browser.MainFrame){
ScriptLoader.ExecuteScript(frame, script.Replace("{width}", ClientSize.Width.ToString()), "screenshot"); ScriptLoader.ExecuteScript(frame, script.Replace("{width}", BrowserUtils.Scale(width, DpiScale).ToString()), "screenshot");
} }
}; };
SetScreenshotHeight(1); SetNotificationSize(width, 1024);
LoadTweet(new TweetNotification(string.Empty, string.Empty, string.Empty, html, 0, string.Empty, string.Empty)); LoadTweet(new TweetNotification(string.Empty, string.Empty, string.Empty, html, 0, string.Empty, string.Empty));
} }
@ -54,14 +53,20 @@ protected override string GetTweetHTML(TweetNotification tweet){
return html; return html;
} }
private void SetScreenshotHeight(int height){ private void SetScreenshotHeight(int browserHeight){
SetNotificationSize(width, height); this.height = BrowserUtils.Scale(browserHeight, SizeScale);
} }
public bool TakeScreenshot(){ public bool TakeScreenshot(bool ignoreHeightError = false){
if (ClientSize.Height == 0){ if (!ignoreHeightError){
FormMessage.Error("Screenshot Failed", "Could not detect screenshot size.", FormMessage.OK); if (height == 0){
return false; FormMessage.Error("Screenshot Failed", "Could not detect screenshot size.", FormMessage.OK);
return false;
}
else if (height > ClientSize.Height){
FormMessage.Error("Screenshot Failed", $"Screenshot is too large: {height}px > {ClientSize.Height}px", FormMessage.OK);
return false;
}
} }
IntPtr context = NativeMethods.GetDC(this.Handle); IntPtr context = NativeMethods.GetDC(this.Handle);
@ -71,7 +76,7 @@ public bool TakeScreenshot(){
return false; return false;
} }
else{ else{
using(Bitmap bmp = new Bitmap(ClientSize.Width, ClientSize.Height, PixelFormat.Format32bppRgb)){ using(Bitmap bmp = new Bitmap(ClientSize.Width, Math.Max(1, height), PixelFormat.Format32bppRgb)){
try{ try{
NativeMethods.RenderSourceIntoBitmap(context, bmp); NativeMethods.RenderSourceIntoBitmap(context, bmp);
}finally{ }finally{

View File

@ -3,7 +3,7 @@
// #define NO_HIDE_SCREENSHOTS // #define NO_HIDE_SCREENSHOTS
// Uncomment to generate screenshots of individual frames for at most 1 second // Uncomment to generate screenshots of individual frames for at most 1 second
#define GEN_SCREENSHOT_FRAMES // #define GEN_SCREENSHOT_FRAMES
#endif #endif
using System; using System;
@ -128,7 +128,7 @@ private void StartDebugger(){
} }
private void debugger_Tick(object sender, EventArgs e){ private void debugger_Tick(object sender, EventArgs e){
if (frameCounter < 63 && screenshot.TakeScreenshot()){ if (frameCounter < 63 && screenshot.TakeScreenshot(true)){
try{ try{
Clipboard.GetImage()?.Save(Path.Combine(DebugScreenshotPath, "frame_"+(++frameCounter)+".png"), ImageFormat.Png); Clipboard.GetImage()?.Save(Path.Combine(DebugScreenshotPath, "frame_"+(++frameCounter)+".png"), ImageFormat.Png);
}catch{ }catch{

View File

@ -10,6 +10,17 @@
let avatarBottom = avatar ? avatar.getBoundingClientRect().bottom : 0; let avatarBottom = avatar ? avatar.getBoundingClientRect().bottom : 0;
$TD.setHeight(Math.floor(Math.max(contentHeight, avatarBottom+9))).then(() => { $TD.setHeight(Math.floor(Math.max(contentHeight, avatarBottom+9))).then(() => {
setTimeout($TD.triggerScreenshot, document.getElementsByTagName("iframe").length ? 267 : 67); let framesLeft = 5; // basic render is done in 1 frame, large media take longer
let onNextFrame = function(){
if (--framesLeft < 0){
$TD.triggerScreenshot();
}
else{
requestAnimationFrame(onNextFrame);
}
};
onNextFrame();
}); });
})($TD_NotificationScreenshot); })($TD_NotificationScreenshot);