From 4b719fee963897fb087e1332008aa63ca631b77a Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 24 Dec 2013 21:19:27 +0200 Subject: [PATCH] Workaround for image resize bug Not using animate window in toast window --- HelpersLib/Helpers/ImageHelpers.cs | 15 ++++++++--- ShareX/Forms/NotificationForm.Designer.cs | 1 - ShareX/Forms/NotificationForm.cs | 32 +++++++---------------- ShareX/TaskManager.cs | 2 +- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/HelpersLib/Helpers/ImageHelpers.cs b/HelpersLib/Helpers/ImageHelpers.cs index d96dc3317..e6d6b4e5b 100644 --- a/HelpersLib/Helpers/ImageHelpers.cs +++ b/HelpersLib/Helpers/ImageHelpers.cs @@ -84,11 +84,20 @@ public static Image ResizeImage(Image img, int width, int height) Bitmap bmp = new Bitmap(width, height, img.PixelFormat); bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution); - using (Graphics g = Graphics.FromImage(bmp)) using (img) + using (Graphics g = Graphics.FromImage(bmp)) { - g.SetHighQuality(); - g.DrawImage(img, 0, 0, width, height); + g.InterpolationMode = InterpolationMode.HighQualityBicubic; + g.SmoothingMode = SmoothingMode.HighQuality; + g.PixelOffsetMode = PixelOffsetMode.HighQuality; + g.CompositingQuality = CompositingQuality.HighQuality; + g.CompositingMode = CompositingMode.SourceOver; + + using (ImageAttributes ia = new ImageAttributes()) + { + ia.SetWrapMode(WrapMode.TileFlipXY); + g.DrawImage(img, new Rectangle(0, 0, width, height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); + } } return bmp; diff --git a/ShareX/Forms/NotificationForm.Designer.cs b/ShareX/Forms/NotificationForm.Designer.cs index 9e3d2ce72..99bbd268a 100644 --- a/ShareX/Forms/NotificationForm.Designer.cs +++ b/ShareX/Forms/NotificationForm.Designer.cs @@ -47,7 +47,6 @@ private void InitializeComponent() this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "ToastForm"; - this.TopMost = true; this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.NotificationForm_MouseClick); this.ResumeLayout(false); diff --git a/ShareX/Forms/NotificationForm.cs b/ShareX/Forms/NotificationForm.cs index 0900b4f79..ea1274365 100644 --- a/ShareX/Forms/NotificationForm.cs +++ b/ShareX/Forms/NotificationForm.cs @@ -45,13 +45,10 @@ public partial class NotificationForm : Form public string URL { get; private set; } private int windowOffset = 3; - private Size maxImageSize; - public NotificationForm(int duration, Size size, string text, Image img, string url) + public NotificationForm(int duration, Size size, Image img, string url) { InitializeComponent(); - maxImageSize = size; - ToastText = text; img = ImageHelpers.ResizeImageLimit(img, size); img = ImageHelpers.DrawCheckers(img); @@ -59,12 +56,9 @@ public NotificationForm(int duration, Size size, string text, Image img, string URL = url; SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); + Size = new Size(img.Width + 2, img.Height + 2); + Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Width - windowOffset, Screen.PrimaryScreen.WorkingArea.Bottom - Height - windowOffset); - Rectangle rect = new Rectangle(Screen.PrimaryScreen.WorkingArea.Right - (img.Width + 2) - windowOffset, - Screen.PrimaryScreen.WorkingArea.Bottom - (img.Height + 2) - windowOffset, img.Width + 2, img.Height + 2); - - NativeMethods.SetWindowPos(Handle, (IntPtr)SpecialWindowHandles.HWND_TOPMOST, rect.X, rect.Y, rect.Width, rect.Height, SetWindowPosFlags.SWP_NOACTIVATE); - NativeMethods.AnimateWindow(Handle, 500, AnimateWindowFlags.AW_SLIDE | AnimateWindowFlags.AW_VER_NEGATIVE); tDuration.Interval = duration; tDuration.Start(); } @@ -72,7 +66,6 @@ public NotificationForm(int duration, Size size, string text, Image img, string private void tDuration_Tick(object sender, EventArgs e) { tDuration.Stop(); - NativeMethods.AnimateWindow(Handle, 1000, AnimateWindowFlags.AW_HIDE | AnimateWindowFlags.AW_BLEND); Close(); } @@ -95,26 +88,21 @@ protected override void OnPaint(PaintEventArgs e) g.DrawRectangleProper(Pens.Black, e.ClipRectangle); } - public static void ShowAsync(string text, string imagePath, string url) + public static void Show(string imagePath, string url) { if (!string.IsNullOrEmpty(imagePath) && File.Exists(imagePath)) { - Thread thread = new Thread(() => - { - using (Image img = ImageHelpers.LoadImage(imagePath)) - using (NotificationForm toastForm = new NotificationForm(5000, new Size(400, 300), text, img, url)) - { - toastForm.ShowDialog(); - } - }); - - thread.Start(); + Image img = ImageHelpers.LoadImage(imagePath); + NotificationForm form = new NotificationForm(5000, new Size(400, 300), img, url); + NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNoActivate); + NativeMethods.SetWindowPos(form.Handle, (IntPtr)SpecialWindowHandles.HWND_TOP, 0, 0, 0, 0, + SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE); } } private void NotificationForm_MouseClick(object sender, MouseEventArgs e) { - if (e.Button == System.Windows.Forms.MouseButtons.Left) + if (e.Button == MouseButtons.Left) { if (!string.IsNullOrEmpty(URL)) { diff --git a/ShareX/TaskManager.cs b/ShareX/TaskManager.cs index 86028ff74..b3cb354f4 100644 --- a/ShareX/TaskManager.cs +++ b/ShareX/TaskManager.cs @@ -324,7 +324,7 @@ private static void task_UploadCompleted(UploadTask task) if (task.Info.TaskSettings.GeneralSettings.ShowToastWindowAfterTask) { - NotificationForm.ShowAsync(balloonTipText, info.FilePath, result); + NotificationForm.Show(info.FilePath, result); } }