Create thumbnail and store it instead of loading full image in picture box to decrease memory usage

This commit is contained in:
Jaex 2019-05-12 23:26:45 +03:00
parent e1f0dd5bc6
commit 0546e1b80d
2 changed files with 51 additions and 17 deletions

View file

@ -30,9 +30,10 @@ private void InitializeComponent()
{
this.pThumbnail = new ShareX.HelpersLib.RoundedCornerPanel();
this.pbProgress = new ShareX.HelpersLib.BlackStyleProgressBar();
this.pbThumbnail = new ShareX.HelpersLib.MyPictureBox();
this.lblFilename = new ShareX.HelpersLib.BlackStyleLabel();
this.pbThumbnail = new System.Windows.Forms.PictureBox();
this.pThumbnail.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pbThumbnail)).BeginInit();
this.SuspendLayout();
//
// pThumbnail
@ -41,8 +42,8 @@ private void InitializeComponent()
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pThumbnail.BackColor = System.Drawing.Color.Transparent;
this.pThumbnail.Controls.Add(this.pbProgress);
this.pThumbnail.Controls.Add(this.pbThumbnail);
this.pThumbnail.Controls.Add(this.pbProgress);
this.pThumbnail.Location = new System.Drawing.Point(0, 24);
this.pThumbnail.Name = "pThumbnail";
this.pThumbnail.Padding = new System.Windows.Forms.Padding(5);
@ -65,17 +66,6 @@ private void InitializeComponent()
this.pbProgress.Value = 50;
this.pbProgress.Visible = false;
//
// pbThumbnail
//
this.pbThumbnail.BackColor = System.Drawing.Color.Transparent;
this.pbThumbnail.Dock = System.Windows.Forms.DockStyle.Fill;
this.pbThumbnail.FullscreenOnClick = true;
this.pbThumbnail.Location = new System.Drawing.Point(5, 5);
this.pbThumbnail.Name = "pbThumbnail";
this.pbThumbnail.PictureBoxBackColor = System.Drawing.Color.Transparent;
this.pbThumbnail.Size = new System.Drawing.Size(246, 246);
this.pbThumbnail.TabIndex = 0;
//
// lblFilename
//
this.lblFilename.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
@ -91,6 +81,16 @@ private void InitializeComponent()
this.lblFilename.Text = "Test.png";
this.lblFilename.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pbThumbnail
//
this.pbThumbnail.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(35)))), ((int)(((byte)(40)))), ((int)(((byte)(48)))));
this.pbThumbnail.Dock = System.Windows.Forms.DockStyle.Fill;
this.pbThumbnail.Location = new System.Drawing.Point(5, 5);
this.pbThumbnail.Name = "pbThumbnail";
this.pbThumbnail.Size = new System.Drawing.Size(246, 246);
this.pbThumbnail.TabIndex = 2;
this.pbThumbnail.TabStop = false;
//
// TaskPanel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -101,6 +101,7 @@ private void InitializeComponent()
this.Name = "TaskPanel";
this.Size = new System.Drawing.Size(256, 280);
this.pThumbnail.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pbThumbnail)).EndInit();
this.ResumeLayout(false);
}
@ -108,8 +109,8 @@ private void InitializeComponent()
#endregion
private HelpersLib.RoundedCornerPanel pThumbnail;
private HelpersLib.MyPictureBox pbThumbnail;
private HelpersLib.BlackStyleLabel lblFilename;
private HelpersLib.BlackStyleProgressBar pbProgress;
private System.Windows.Forms.PictureBox pbThumbnail;
}
}

View file

@ -32,6 +32,7 @@ You should have received a copy of the GNU General Public License
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShareX.HelpersLib;
namespace ShareX
{
@ -96,6 +97,10 @@ public bool ProgressVisible
private bool progressVisible;
public Image ThumbnailImage { get; private set; }
public Size ThumbnailSize { get; private set; }
public string ThumbnailSourceFilePath { get; private set; }
public new event MouseEventHandler MouseDown
{
add
@ -142,12 +147,13 @@ public TaskPanel(WorkerTask task)
Task = task;
UpdateFilename();
UpdateThumbnail();
}
public void ChangeThumbnailSize(Size size)
{
Size = new Size(pThumbnail.Padding.Horizontal + size.Width, pThumbnail.Top + pThumbnail.Padding.Vertical + size.Height);
ThumbnailSize = size;
Size = new Size(pThumbnail.Padding.Horizontal + ThumbnailSize.Width, pThumbnail.Top + pThumbnail.Padding.Vertical + ThumbnailSize.Height);
UpdateThumbnail();
}
public void UpdateFilename()
@ -157,7 +163,34 @@ public void UpdateFilename()
public void UpdateThumbnail()
{
pbThumbnail.LoadImageFromFileAsync(Task.Info.FilePath);
ClearThumbnail();
if (!ThumbnailSize.IsEmpty && Task.Info != null)
{
string filePath = Task.Info.FilePath;
using (Image img = ImageHelpers.LoadImage(filePath))
{
if (img != null)
{
ThumbnailSourceFilePath = filePath;
ThumbnailImage = ImageHelpers.CreateThumbnail(img, ThumbnailSize.Width, ThumbnailSize.Height);
pbThumbnail.Image = ThumbnailImage;
}
}
}
}
public void ClearThumbnail()
{
pbThumbnail.Image = null;
if (ThumbnailImage != null)
{
ThumbnailImage.Dispose();
ThumbnailImage = null;
}
}
public void UpdateProgress()