From 09cd2e62fd3352efcaad39cef981b8b98cf116ba Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 2 Jan 2019 11:58:31 +0300 Subject: [PATCH] Added quality option to image thumbnailer --- ShareX.HelpersLib/DNS/DNSChangerForm.resx | 6 +- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 6 - .../Forms/ImageThumbnailerForm.Designer.cs | 34 ++++- ShareX.MediaLib/Forms/ImageThumbnailerForm.cs | 65 ++++++---- .../Forms/ImageThumbnailerForm.resx | 121 +++++++++++++++--- 5 files changed, 178 insertions(+), 54 deletions(-) diff --git a/ShareX.HelpersLib/DNS/DNSChangerForm.resx b/ShareX.HelpersLib/DNS/DNSChangerForm.resx index 423365376..c149696aa 100644 --- a/ShareX.HelpersLib/DNS/DNSChangerForm.resx +++ b/ShareX.HelpersLib/DNS/DNSChangerForm.resx @@ -397,7 +397,7 @@ Ping - @Invariant + btnPingPrimary @@ -421,7 +421,7 @@ Ping - @Invariant + btnPingSecondary @@ -448,7 +448,7 @@ CenterScreen - ShareX - DNS Changer + ShareX - DNS changer DNSChangerForm diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index c08a3820a..fa8ec1fcc 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -142,11 +142,6 @@ public static Image ResizeImage(Image img, int width, int height, bool allowEnla public static Image CreateThumbnail(Image img, int width, int height) { - if (img.Width == width && img.Height == height) - { - return img; - } - double srcRatio = (double)img.Width / img.Height; double dstRatio = (double)width / height; int w, h; @@ -185,7 +180,6 @@ public static Image CreateThumbnail(Image img, int width, int height) bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution); using (Graphics g = Graphics.FromImage(bmp)) - using (img) { g.SetHighQuality(); g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, w, h), GraphicsUnit.Pixel); diff --git a/ShareX.MediaLib/Forms/ImageThumbnailerForm.Designer.cs b/ShareX.MediaLib/Forms/ImageThumbnailerForm.Designer.cs index f48d8a721..8b8c129f8 100644 --- a/ShareX.MediaLib/Forms/ImageThumbnailerForm.Designer.cs +++ b/ShareX.MediaLib/Forms/ImageThumbnailerForm.Designer.cs @@ -43,8 +43,12 @@ private void InitializeComponent() this.lblOutputFolder = new System.Windows.Forms.Label(); this.txtOutputFolder = new System.Windows.Forms.TextBox(); this.btnOutputFolder = new System.Windows.Forms.Button(); + this.lblQuality = new System.Windows.Forms.Label(); + this.nudQuality = new System.Windows.Forms.NumericUpDown(); + this.lblQualityPercentage = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.nudWidth)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nudHeight)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudQuality)).BeginInit(); this.SuspendLayout(); // // lvImages @@ -60,6 +64,7 @@ private void InitializeComponent() this.lvImages.Name = "lvImages"; this.lvImages.UseCompatibleStateImageBehavior = false; this.lvImages.View = System.Windows.Forms.View.Details; + this.lvImages.SelectedIndexChanged += new System.EventHandler(this.lvImages_SelectedIndexChanged); this.lvImages.DragDrop += new System.Windows.Forms.DragEventHandler(this.lvImages_DragDrop); this.lvImages.DragEnter += new System.Windows.Forms.DragEventHandler(this.lvImages_DragEnter); // @@ -117,7 +122,7 @@ private void InitializeComponent() 0, 0, 0}); - this.nudHeight.ValueChanged += new System.EventHandler(this.nudWidth_ValueChanged); + this.nudHeight.ValueChanged += new System.EventHandler(this.nudHeight_ValueChanged); // // lblOutputFilename // @@ -155,11 +160,34 @@ private void InitializeComponent() this.btnOutputFolder.UseVisualStyleBackColor = true; this.btnOutputFolder.Click += new System.EventHandler(this.btnOutputFolder_Click); // + // lblQuality + // + resources.ApplyResources(this.lblQuality, "lblQuality"); + this.lblQuality.Name = "lblQuality"; + // + // nudQuality + // + resources.ApplyResources(this.nudQuality, "nudQuality"); + this.nudQuality.Name = "nudQuality"; + this.nudQuality.Value = new decimal(new int[] { + 90, + 0, + 0, + 0}); + // + // lblQualityPercentage + // + resources.ApplyResources(this.lblQualityPercentage, "lblQualityPercentage"); + this.lblQualityPercentage.Name = "lblQualityPercentage"; + // // ImageThumbnailerForm // this.AcceptButton = this.btnGenerate; resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.lblQualityPercentage); + this.Controls.Add(this.nudQuality); + this.Controls.Add(this.lblQuality); this.Controls.Add(this.btnOutputFolder); this.Controls.Add(this.txtOutputFolder); this.Controls.Add(this.lblOutputFolder); @@ -178,6 +206,7 @@ private void InitializeComponent() this.Name = "ImageThumbnailerForm"; ((System.ComponentModel.ISupportInitialize)(this.nudWidth)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nudHeight)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudQuality)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -199,5 +228,8 @@ private void InitializeComponent() private System.Windows.Forms.Label lblOutputFolder; private System.Windows.Forms.TextBox txtOutputFolder; private System.Windows.Forms.Button btnOutputFolder; + private System.Windows.Forms.Label lblQuality; + private System.Windows.Forms.NumericUpDown nudQuality; + private System.Windows.Forms.Label lblQualityPercentage; } } \ No newline at end of file diff --git a/ShareX.MediaLib/Forms/ImageThumbnailerForm.cs b/ShareX.MediaLib/Forms/ImageThumbnailerForm.cs index 339f2b9b0..cf7994a23 100644 --- a/ShareX.MediaLib/Forms/ImageThumbnailerForm.cs +++ b/ShareX.MediaLib/Forms/ImageThumbnailerForm.cs @@ -39,27 +39,24 @@ public ImageThumbnailerForm() Icon = ShareXResources.Icon; } - private void CheckState() + private void UpdateEnabled() { + btnRemove.Enabled = lvImages.SelectedItems.Count > 0; btnGenerate.Enabled = lvImages.Items.Count > 0 && nudWidth.Value > 0 && nudHeight.Value > 0 && !string.IsNullOrEmpty(txtOutputFolder.Text) && !string.IsNullOrEmpty(txtOutputFilename.Text); } private void AddFile(string filePath) { - if (string.IsNullOrEmpty(filePath)) + if (!string.IsNullOrEmpty(filePath)) { - return; + lvImages.Items.Add(filePath); + + if (string.IsNullOrEmpty(txtOutputFolder.Text)) + { + txtOutputFolder.Text = Path.GetDirectoryName(filePath); + } } - - lvImages.Items.Add(filePath); - - if (string.IsNullOrEmpty(txtOutputFolder.Text)) - { - txtOutputFolder.Text = Path.GetDirectoryName(filePath); - } - - CheckState(); } private void btnAdd_Click(object sender, EventArgs e) @@ -72,6 +69,8 @@ private void btnAdd_Click(object sender, EventArgs e) { AddFile(image); } + + UpdateEnabled(); } } @@ -86,6 +85,11 @@ private void btnRemove_Click(object sender, EventArgs e) } } + private void lvImages_SelectedIndexChanged(object sender, EventArgs e) + { + UpdateEnabled(); + } + private void lvImages_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false)) @@ -110,18 +114,25 @@ private void lvImages_DragDrop(object sender, DragEventArgs e) { AddFile(file); } + + UpdateEnabled(); } } } private void nudWidth_ValueChanged(object sender, EventArgs e) { - CheckState(); + UpdateEnabled(); + } + + private void nudHeight_ValueChanged(object sender, EventArgs e) + { + UpdateEnabled(); } private void txtOutputFolder_TextChanged(object sender, EventArgs e) { - CheckState(); + UpdateEnabled(); } private void btnOutputFolder_Click(object sender, EventArgs e) @@ -131,7 +142,7 @@ private void btnOutputFolder_Click(object sender, EventArgs e) private void txtOutputFilename_TextChanged(object sender, EventArgs e) { - CheckState(); + UpdateEnabled(); } private void btnGenerate_Click(object sender, EventArgs e) @@ -140,9 +151,12 @@ private void btnGenerate_Click(object sender, EventArgs e) { int width = (int)nudWidth.Value; int height = (int)nudHeight.Value; + int quality = (int)nudQuality.Value; string outputFolder = txtOutputFolder.Text; string outputFilename = txtOutputFilename.Text; + Cursor = Cursors.WaitCursor; + try { foreach (ListViewItem lvi in lvImages.Items) @@ -151,16 +165,17 @@ private void btnGenerate_Click(object sender, EventArgs e) if (File.Exists(filePath)) { - Image img = ImageHelpers.LoadImage(filePath); - - if (img != null) + using (Image img = ImageHelpers.LoadImage(filePath)) { - using (img = ImageHelpers.CreateThumbnail(img, width, height)) + if (img != null) { - string filename = Path.GetFileNameWithoutExtension(filePath); - string outputPath = Path.Combine(outputFolder, outputFilename.Replace("$filename", filename)); - outputPath = Path.ChangeExtension(outputPath, "jpg"); - img.SaveJPG(outputPath, 90); + using (Image thumbnail = ImageHelpers.CreateThumbnail(img, width, height)) + { + string filename = Path.GetFileNameWithoutExtension(filePath); + string outputPath = Path.Combine(outputFolder, outputFilename.Replace("$filename", filename)); + outputPath = Path.ChangeExtension(outputPath, "jpg"); + thumbnail.SaveJPG(outputPath, quality); + } } } } @@ -171,6 +186,10 @@ private void btnGenerate_Click(object sender, EventArgs e) DebugHelper.WriteException(ex); ex.ShowError(); } + finally + { + Cursor = Cursors.Default; + } } } } diff --git a/ShareX.MediaLib/Forms/ImageThumbnailerForm.resx b/ShareX.MediaLib/Forms/ImageThumbnailerForm.resx index 224c48c2c..e05e17ff0 100644 --- a/ShareX.MediaLib/Forms/ImageThumbnailerForm.resx +++ b/ShareX.MediaLib/Forms/ImageThumbnailerForm.resx @@ -132,19 +132,19 @@ lvImages - ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + ShareX.HelpersLib.MyListView, ShareX.HelpersLib, Version=12.4.0.0, Culture=neutral, PublicKeyToken=null $this - 12 + 15 8, 8 - 168, 24 + 152, 24 1 @@ -162,13 +162,16 @@ $this - 11 + 14 + + + False - 184, 8 + 168, 8 - 168, 23 + 152, 24 2 @@ -186,13 +189,13 @@ $this - 10 + 13 True - 8, 416 + 5, 416 38, 13 @@ -213,7 +216,7 @@ $this - 9 + 12 8, 432 @@ -238,13 +241,13 @@ $this - 8 + 11 True - 104, 416 + 101, 416 41, 13 @@ -265,7 +268,7 @@ $this - 7 + 10 104, 432 @@ -289,7 +292,7 @@ $this - 6 + 9 True @@ -316,7 +319,7 @@ $this - 5 + 8 8, 528 @@ -329,7 +332,6 @@ $filename_th - @Invariant txtOutputFilename @@ -341,7 +343,7 @@ $this - 4 + 7 False @@ -350,7 +352,7 @@ 8, 560 - 560, 23 + 560, 24 11 @@ -368,7 +370,7 @@ $this - 3 + 6 True @@ -395,7 +397,7 @@ $this - 2 + 5 8, 480 @@ -416,7 +418,7 @@ $this - 1 + 4 536, 479 @@ -429,7 +431,6 @@ ... - @Invariant btnOutputFolder @@ -441,6 +442,84 @@ $this + 3 + + + True + + + 197, 416 + + + 42, 13 + + + 15 + + + Quality: + + + lblQuality + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 2 + + + 200, 432 + + + 56, 20 + + + 16 + + + Center + + + nudQuality + + + System.Windows.Forms.NumericUpDown, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + True + + + 261, 436 + + + 15, 13 + + + 17 + + + % + + + lblQualityPercentage + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + 0