From fd72ce232bcb9c5014ec39825ea3fd733f5aa1c9 Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 6 Mar 2017 07:30:41 +0200 Subject: [PATCH 1/8] Edit gitignore list --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 96374c4..c0d8376 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk +/.vs +/AmazonEncoder/obj From f970d13b3ed5ccde0c0cd0e4367794000d17b546 Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 6 Mar 2017 08:40:08 +0200 Subject: [PATCH 2/8] Clean namespaces --- AmazonEncoder/AmazonEncoder.csproj.user | 3 +++ AmazonEncoder/Form1.cs | 7 +------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/AmazonEncoder/AmazonEncoder.csproj.user b/AmazonEncoder/AmazonEncoder.csproj.user index 7e8fe36..a4a6cdf 100644 --- a/AmazonEncoder/AmazonEncoder.csproj.user +++ b/AmazonEncoder/AmazonEncoder.csproj.user @@ -1,3 +1,6 @@  + + ProjectFiles + \ No newline at end of file diff --git a/AmazonEncoder/Form1.cs b/AmazonEncoder/Form1.cs index 932aaf9..39b8b17 100644 --- a/AmazonEncoder/Form1.cs +++ b/AmazonEncoder/Form1.cs @@ -1,12 +1,7 @@ using System; using System.IO; -using System.Data; -using System.Text; -using System.Drawing; using System.Threading; using System.Windows.Forms; -using System.ComponentModel; -using System.Collections.Generic; namespace AmazonEncoder { @@ -25,7 +20,7 @@ private void button1_Click(object sender, EventArgs e) OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; - if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) + if (ofd.ShowDialog() == DialogResult.OK) { int count = ofd.FileNames.GetLength(0); // Gets the number of files selected label2.Text = String.Format("0 of {0} Completed", count); // Updates the form to show starting status From 4247682f91f3605d52b60c11b32072b24e11e586 Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 6 Mar 2017 08:41:55 +0200 Subject: [PATCH 3/8] General coding style changes. --- AmazonEncoder/Form1.cs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/AmazonEncoder/Form1.cs b/AmazonEncoder/Form1.cs index 39b8b17..5eeb506 100644 --- a/AmazonEncoder/Form1.cs +++ b/AmazonEncoder/Form1.cs @@ -13,22 +13,19 @@ public Form1() } // Represents the first bytes of a PNG file - byte[] pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; + readonly byte[] _pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; private void button1_Click(object sender, EventArgs e) { - OpenFileDialog ofd = new OpenFileDialog(); - ofd.Multiselect = true; - if (ofd.ShowDialog() == DialogResult.OK) - { - int count = ofd.FileNames.GetLength(0); // Gets the number of files selected - label2.Text = String.Format("0 of {0} Completed", count); // Updates the form to show starting status - progressBar1.Value = 0; // Reset the progress bar's value to 0 in case there is multiple uses in this instance - progressBar1.Maximum = count; // Sets the maximum of the progress bar to the number of files selected - Thread x = new Thread(() => EncodeFiles(ofd.FileNames, count, checkBox1.Checked)); // Creates the new thread which encodes the files - x.Start(); // Starts the thread - } + OpenFileDialog ofd = new OpenFileDialog {Multiselect = true}; + if (ofd.ShowDialog() != DialogResult.OK) return; + int count = ofd.FileNames.GetLength(0); // Gets the number of files selected + label2.Text = $@"0 of {count} Completed"; // Updates the form to show starting status + progressBar1.Value = 0; // Reset the progress bar's value to 0 in case there is multiple uses in this instance + progressBar1.Maximum = count; // Sets the maximum of the progress bar to the number of files selected + Thread x = new Thread(() => EncodeFiles(ofd.FileNames, count, checkBox1.Checked)); // Creates the new thread which encodes the files + x.Start(); // Starts the thread } void EncodeFiles(string[] filepaths, int total, bool delete) @@ -38,9 +35,9 @@ void EncodeFiles(string[] filepaths, int total, bool delete) { FileInfo fi = new FileInfo(f); // Used for getting the original file name byte[] b = File.ReadAllBytes(f); // Get original files' bytes - byte[] ret = new byte[pngHeader.Length + b.Length]; // Create a new byte array the length of the PNG header + the length of original file - Buffer.BlockCopy(pngHeader, 0, ret, 0, pngHeader.Length); // Copies the PNG header bytes to the beginning of the new byte array - Buffer.BlockCopy(b, 0, ret, pngHeader.Length, b.Length); // Copies the original file bytes to into the new byte array, after the PNG header + byte[] ret = new byte[_pngHeader.Length + b.Length]; // Create a new byte array the length of the PNG header + the length of original file + Buffer.BlockCopy(_pngHeader, 0, ret, 0, _pngHeader.Length); // Copies the PNG header bytes to the beginning of the new byte array + Buffer.BlockCopy(b, 0, ret, _pngHeader.Length, b.Length); // Copies the original file bytes to into the new byte array, after the PNG header string adrive = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Amazon Drive\"); // Get Amazon Drive path string newname = String.Concat(adrive, fi.Name, "-encoded.png"); // Create new name based on old file @@ -57,12 +54,13 @@ void EncodeFiles(string[] filepaths, int total, bool delete) { done += 1; progressBar1.Value += 1; - label2.Text = String.Format("{0} of {1} Completed", done, total); + label2.Text = $@"{done} of {total} Completed"; })); } // Notify the user of completion - Invoke(new MethodInvoker(() => MessageBox.Show(String.Format("{0} files have been encoded and written to the Amazon Drive folder.", total), "Done", MessageBoxButtons.OK, MessageBoxIcon.Information))); + Invoke(new MethodInvoker(() => MessageBox.Show( + $@"{total} files have been encoded and written to the Amazon Drive folder.", @"Done", MessageBoxButtons.OK, MessageBoxIcon.Information))); } } } From 81539de663bcffc628f7cfa87e6052be0b0ed2a9 Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 6 Mar 2017 09:15:56 +0200 Subject: [PATCH 4/8] Convert from Thread to BackgroundWorker --- AmazonEncoder/Form1.cs | 72 ++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/AmazonEncoder/Form1.cs b/AmazonEncoder/Form1.cs index 5eeb506..96c4efc 100644 --- a/AmazonEncoder/Form1.cs +++ b/AmazonEncoder/Form1.cs @@ -1,15 +1,22 @@ using System; using System.IO; -using System.Threading; using System.Windows.Forms; +using System.ComponentModel; namespace AmazonEncoder { public partial class Form1 : Form { + private readonly BackgroundWorker _worker; + private OpenFileDialog _fileDialog; public Form1() { InitializeComponent(); + _worker= new BackgroundWorker(); + _worker.DoWork += StartEncoding; + _worker.ProgressChanged += ProgressChanged; + _worker.RunWorkerCompleted += FinishEncoding; + _fileDialog = new OpenFileDialog {Multiselect = true}; } // Represents the first bytes of a PNG file @@ -17,50 +24,47 @@ public Form1() private void button1_Click(object sender, EventArgs e) { - - OpenFileDialog ofd = new OpenFileDialog {Multiselect = true}; - if (ofd.ShowDialog() != DialogResult.OK) return; - int count = ofd.FileNames.GetLength(0); // Gets the number of files selected + if (_fileDialog.ShowDialog(this) != DialogResult.OK) return; + int count = _fileDialog.FileNames.GetLength(0); // Gets the number of files selected label2.Text = $@"0 of {count} Completed"; // Updates the form to show starting status progressBar1.Value = 0; // Reset the progress bar's value to 0 in case there is multiple uses in this instance progressBar1.Maximum = count; // Sets the maximum of the progress bar to the number of files selected - Thread x = new Thread(() => EncodeFiles(ofd.FileNames, count, checkBox1.Checked)); // Creates the new thread which encodes the files - x.Start(); // Starts the thread + progressBar1.Step = 1; + checkBox1.Enabled = false; + button1.Enabled = false; + _worker.RunWorkerAsync(_fileDialog.FileNames); } - void EncodeFiles(string[] filepaths, int total, bool delete) + private void FinishEncoding(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs) { - int done = 0; // Used to show how many files we've completed - foreach (string f in filepaths) - { - FileInfo fi = new FileInfo(f); // Used for getting the original file name - byte[] b = File.ReadAllBytes(f); // Get original files' bytes - byte[] ret = new byte[_pngHeader.Length + b.Length]; // Create a new byte array the length of the PNG header + the length of original file - Buffer.BlockCopy(_pngHeader, 0, ret, 0, _pngHeader.Length); // Copies the PNG header bytes to the beginning of the new byte array - Buffer.BlockCopy(b, 0, ret, _pngHeader.Length, b.Length); // Copies the original file bytes to into the new byte array, after the PNG header + checkBox1.Enabled = true; + button1.Enabled = true; + MessageBox.Show(this, @"All files have been encoded and written to the Amazon Drive folder.", @"Done", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } - string adrive = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Amazon Drive\"); // Get Amazon Drive path - string newname = String.Concat(adrive, fi.Name, "-encoded.png"); // Create new name based on old file - File.WriteAllBytes(newname, ret); // Write new file to Amazon Drive folder + private void ProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs) + { + progressBar1.PerformStep(); + label2.Text = progressBar1.Value+label2.Text.Substring(label2.Text.IndexOf(' ')); + } - // Delete old file if necessary - if (delete) + private void StartEncoding(object sender, DoWorkEventArgs doWorkEventArgs) + { + string adrive = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Amazon Drive\"); // Get Amazon Drive path + foreach (string f in (string[])doWorkEventArgs.Argument) + { + FileInfo fi = new FileInfo(f); // Used for getting the original file name + using (var fs = File.OpenWrite($"{adrive}{fi.Name}-encoded.png")) { - fi.Delete(); + fs.Write(_pngHeader, 0, _pngHeader.Length); + fs.Write(File.ReadAllBytes(f),0,(int)fi.Length); + if(checkBox1.Checked) + fi.Delete(); } - - // Updates the form to show status - Invoke(new MethodInvoker(() => - { - done += 1; - progressBar1.Value += 1; - label2.Text = $@"{done} of {total} Completed"; - })); } - - // Notify the user of completion - Invoke(new MethodInvoker(() => MessageBox.Show( - $@"{total} files have been encoded and written to the Amazon Drive folder.", @"Done", MessageBoxButtons.OK, MessageBoxIcon.Information))); + _worker.ReportProgress(1); } } } + From 97a75abd8f3cbee350a327b8b03fe895912ba08e Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 6 Mar 2017 09:23:28 +0200 Subject: [PATCH 5/8] General naming and style changes. --- ....Designer.cs => AmazonEncoder.Designer.cs} | 68 +++++++++---------- AmazonEncoder/{Form1.cs => AmazonEncoder.cs} | 40 +++++------ AmazonEncoder/AmazonEncoder.csproj | 10 +-- .../{Form1.resx => AmazonEncoder.resx} | 0 AmazonEncoder/Program.cs | 2 +- 5 files changed, 61 insertions(+), 59 deletions(-) rename AmazonEncoder/{Form1.Designer.cs => AmazonEncoder.Designer.cs} (60%) rename AmazonEncoder/{Form1.cs => AmazonEncoder.cs} (64%) rename AmazonEncoder/{Form1.resx => AmazonEncoder.resx} (100%) diff --git a/AmazonEncoder/Form1.Designer.cs b/AmazonEncoder/AmazonEncoder.Designer.cs similarity index 60% rename from AmazonEncoder/Form1.Designer.cs rename to AmazonEncoder/AmazonEncoder.Designer.cs index 1dde4f0..245cbb7 100644 --- a/AmazonEncoder/Form1.Designer.cs +++ b/AmazonEncoder/AmazonEncoder.Designer.cs @@ -1,6 +1,6 @@ namespace AmazonEncoder { - partial class Form1 + partial class AmazonEncoder { /// /// Required designer variable. @@ -28,23 +28,23 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); - this.button1 = new System.Windows.Forms.Button(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(AmazonEncoder)); + this.btn_EncodeFiles = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); - this.progressBar1 = new System.Windows.Forms.ProgressBar(); + this.progressBar = new System.Windows.Forms.ProgressBar(); this.label2 = new System.Windows.Forms.Label(); - this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.chk_DeleteOriginal = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // - // button1 + // btn_EncodeFiles // - this.button1.Location = new System.Drawing.Point(14, 14); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(303, 27); - this.button1.TabIndex = 0; - this.button1.Text = "Encode Files"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.btn_EncodeFiles.Location = new System.Drawing.Point(14, 14); + this.btn_EncodeFiles.Name = "btn_EncodeFiles"; + this.btn_EncodeFiles.Size = new System.Drawing.Size(303, 27); + this.btn_EncodeFiles.TabIndex = 0; + this.btn_EncodeFiles.Text = "Encode Files"; + this.btn_EncodeFiles.UseVisualStyleBackColor = true; + this.btn_EncodeFiles.Click += new System.EventHandler(this.btn_EncodeFiles_Click); // // label1 // @@ -55,12 +55,12 @@ private void InitializeComponent() this.label1.TabIndex = 1; this.label1.Text = "Progress:"; // - // progressBar1 + // progressBar // - this.progressBar1.Location = new System.Drawing.Point(12, 77); - this.progressBar1.Name = "progressBar1"; - this.progressBar1.Size = new System.Drawing.Size(307, 23); - this.progressBar1.TabIndex = 2; + this.progressBar.Location = new System.Drawing.Point(12, 77); + this.progressBar.Name = "progressBar"; + this.progressBar.Size = new System.Drawing.Size(307, 23); + this.progressBar.TabIndex = 2; // // label2 // @@ -70,31 +70,31 @@ private void InitializeComponent() this.label2.TabIndex = 3; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // - // checkBox1 + // chk_DeleteOriginal // - this.checkBox1.AutoSize = true; - this.checkBox1.Location = new System.Drawing.Point(194, 47); - this.checkBox1.Name = "checkBox1"; - this.checkBox1.Size = new System.Drawing.Size(125, 19); - this.checkBox1.TabIndex = 4; - this.checkBox1.Text = "Delete Original File"; - this.checkBox1.UseVisualStyleBackColor = true; + this.chk_DeleteOriginal.AutoSize = true; + this.chk_DeleteOriginal.Location = new System.Drawing.Point(194, 47); + this.chk_DeleteOriginal.Name = "chk_DeleteOriginal"; + this.chk_DeleteOriginal.Size = new System.Drawing.Size(125, 19); + this.chk_DeleteOriginal.TabIndex = 4; + this.chk_DeleteOriginal.Text = "Delete Original File"; + this.chk_DeleteOriginal.UseVisualStyleBackColor = true; // - // Form1 + // AmazonEncoder // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(331, 133); - this.Controls.Add(this.checkBox1); + this.Controls.Add(this.chk_DeleteOriginal); this.Controls.Add(this.label2); - this.Controls.Add(this.progressBar1); + this.Controls.Add(this.progressBar); this.Controls.Add(this.label1); - this.Controls.Add(this.button1); + this.Controls.Add(this.btn_EncodeFiles); this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; - this.Name = "Form1"; + this.Name = "AmazonEncoder"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Amazon Drive Encoder"; this.ResumeLayout(false); @@ -104,11 +104,11 @@ private void InitializeComponent() #endregion - private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button btn_EncodeFiles; private System.Windows.Forms.Label label1; - private System.Windows.Forms.ProgressBar progressBar1; + private System.Windows.Forms.ProgressBar progressBar; private System.Windows.Forms.Label label2; - private System.Windows.Forms.CheckBox checkBox1; + private System.Windows.Forms.CheckBox chk_DeleteOriginal; } } diff --git a/AmazonEncoder/Form1.cs b/AmazonEncoder/AmazonEncoder.cs similarity index 64% rename from AmazonEncoder/Form1.cs rename to AmazonEncoder/AmazonEncoder.cs index 96c4efc..9cfcc1e 100644 --- a/AmazonEncoder/Form1.cs +++ b/AmazonEncoder/AmazonEncoder.cs @@ -5,11 +5,17 @@ namespace AmazonEncoder { - public partial class Form1 : Form + public partial class AmazonEncoder : Form { private readonly BackgroundWorker _worker; - private OpenFileDialog _fileDialog; - public Form1() + private readonly OpenFileDialog _fileDialog; + private readonly string _amazonDrive = + $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\Amazon Drive\\"; // Get Amazon Drive path + + // Represents the first bytes of a PNG file + readonly byte[] _pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; + + public AmazonEncoder() { InitializeComponent(); _worker= new BackgroundWorker(); @@ -17,49 +23,45 @@ public Form1() _worker.ProgressChanged += ProgressChanged; _worker.RunWorkerCompleted += FinishEncoding; _fileDialog = new OpenFileDialog {Multiselect = true}; + progressBar.Step = 1; } - // Represents the first bytes of a PNG file - readonly byte[] _pngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; - - private void button1_Click(object sender, EventArgs e) + private void btn_EncodeFiles_Click(object sender, EventArgs e) { if (_fileDialog.ShowDialog(this) != DialogResult.OK) return; int count = _fileDialog.FileNames.GetLength(0); // Gets the number of files selected label2.Text = $@"0 of {count} Completed"; // Updates the form to show starting status - progressBar1.Value = 0; // Reset the progress bar's value to 0 in case there is multiple uses in this instance - progressBar1.Maximum = count; // Sets the maximum of the progress bar to the number of files selected - progressBar1.Step = 1; - checkBox1.Enabled = false; - button1.Enabled = false; + progressBar.Value = 0; // Reset the progress bar's value to 0 in case there is multiple uses in this instance + progressBar.Maximum = count; // Sets the maximum of the progress bar to the number of files selected + chk_DeleteOriginal.Enabled = false; + btn_EncodeFiles.Enabled = false; _worker.RunWorkerAsync(_fileDialog.FileNames); } private void FinishEncoding(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs) { - checkBox1.Enabled = true; - button1.Enabled = true; + chk_DeleteOriginal.Enabled = true; + btn_EncodeFiles.Enabled = true; MessageBox.Show(this, @"All files have been encoded and written to the Amazon Drive folder.", @"Done", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void ProgressChanged(object sender, ProgressChangedEventArgs progressChangedEventArgs) { - progressBar1.PerformStep(); - label2.Text = progressBar1.Value+label2.Text.Substring(label2.Text.IndexOf(' ')); + progressBar.PerformStep(); + label2.Text = progressBar.Value+label2.Text.Substring(label2.Text.IndexOf(' ')); } private void StartEncoding(object sender, DoWorkEventArgs doWorkEventArgs) { - string adrive = String.Concat(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\Amazon Drive\"); // Get Amazon Drive path foreach (string f in (string[])doWorkEventArgs.Argument) { FileInfo fi = new FileInfo(f); // Used for getting the original file name - using (var fs = File.OpenWrite($"{adrive}{fi.Name}-encoded.png")) + using (var fs = File.OpenWrite($"{_amazonDrive}{fi.Name}-encoded.png")) { fs.Write(_pngHeader, 0, _pngHeader.Length); fs.Write(File.ReadAllBytes(f),0,(int)fi.Length); - if(checkBox1.Checked) + if(chk_DeleteOriginal.Checked) fi.Delete(); } } diff --git a/AmazonEncoder/AmazonEncoder.csproj b/AmazonEncoder/AmazonEncoder.csproj index 77e4b5a..4c2b411 100644 --- a/AmazonEncoder/AmazonEncoder.csproj +++ b/AmazonEncoder/AmazonEncoder.csproj @@ -61,16 +61,16 @@ - + Form - - Form1.cs + + AmazonEncoder.cs - - Form1.cs + + AmazonEncoder.cs ResXFileCodeGenerator diff --git a/AmazonEncoder/Form1.resx b/AmazonEncoder/AmazonEncoder.resx similarity index 100% rename from AmazonEncoder/Form1.resx rename to AmazonEncoder/AmazonEncoder.resx diff --git a/AmazonEncoder/Program.cs b/AmazonEncoder/Program.cs index e486e49..4d368a6 100644 --- a/AmazonEncoder/Program.cs +++ b/AmazonEncoder/Program.cs @@ -14,7 +14,7 @@ static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); + Application.Run(new AmazonEncoder()); } } } From 62432c07c531113fa3ff2db044962d378cbae4f6 Mon Sep 17 00:00:00 2001 From: Tarek Date: Tue, 7 Mar 2017 07:02:21 +0200 Subject: [PATCH 6/8] Fix background worker not able to report progress. --- .gitignore | 1 + AmazonEncoder/AmazonEncoder.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index c0d8376..4fd0f0e 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ Temporary Items .apdisk /.vs /AmazonEncoder/obj +/AmazonEncoder/bin/x86 diff --git a/AmazonEncoder/AmazonEncoder.cs b/AmazonEncoder/AmazonEncoder.cs index 9cfcc1e..a0f4079 100644 --- a/AmazonEncoder/AmazonEncoder.cs +++ b/AmazonEncoder/AmazonEncoder.cs @@ -22,6 +22,7 @@ public AmazonEncoder() _worker.DoWork += StartEncoding; _worker.ProgressChanged += ProgressChanged; _worker.RunWorkerCompleted += FinishEncoding; + _worker.WorkerReportsProgress = true; _fileDialog = new OpenFileDialog {Multiselect = true}; progressBar.Step = 1; } From 705943edb620dcc0f18e12037755cde801c02e15 Mon Sep 17 00:00:00 2001 From: Tarek Date: Tue, 7 Mar 2017 07:10:54 +0200 Subject: [PATCH 7/8] fix loading whole file in memory. --- AmazonEncoder/AmazonEncoder.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/AmazonEncoder/AmazonEncoder.cs b/AmazonEncoder/AmazonEncoder.cs index a0f4079..3390bc6 100644 --- a/AmazonEncoder/AmazonEncoder.cs +++ b/AmazonEncoder/AmazonEncoder.cs @@ -55,13 +55,19 @@ private void ProgressChanged(object sender, ProgressChangedEventArgs progressCha private void StartEncoding(object sender, DoWorkEventArgs doWorkEventArgs) { + byte[] buffer = new byte[4096]; foreach (string f in (string[])doWorkEventArgs.Argument) { FileInfo fi = new FileInfo(f); // Used for getting the original file name using (var fs = File.OpenWrite($"{_amazonDrive}{fi.Name}-encoded.png")) { fs.Write(_pngHeader, 0, _pngHeader.Length); - fs.Write(File.ReadAllBytes(f),0,(int)fi.Length); + using (var inFile = File.OpenRead(fi.FullName)) + { + int count; + while ((count = inFile.Read(buffer, 0, buffer.Length)) != 0) + fs.Write(buffer, 0, count); + } if(chk_DeleteOriginal.Checked) fi.Delete(); } From c5d593296ba27c68534cc927e279a0cd34b1aa56 Mon Sep 17 00:00:00 2001 From: Tarek Date: Tue, 7 Mar 2017 07:11:15 +0200 Subject: [PATCH 8/8] fix wrong progress report --- AmazonEncoder/AmazonEncoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AmazonEncoder/AmazonEncoder.cs b/AmazonEncoder/AmazonEncoder.cs index 3390bc6..d445047 100644 --- a/AmazonEncoder/AmazonEncoder.cs +++ b/AmazonEncoder/AmazonEncoder.cs @@ -71,8 +71,8 @@ private void StartEncoding(object sender, DoWorkEventArgs doWorkEventArgs) if(chk_DeleteOriginal.Checked) fi.Delete(); } + _worker.ReportProgress(1); } - _worker.ReportProgress(1); } } }