diff --git a/.gitignore b/.gitignore index 96374c4..4fd0f0e 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk +/.vs +/AmazonEncoder/obj +/AmazonEncoder/bin/x86 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/AmazonEncoder.cs b/AmazonEncoder/AmazonEncoder.cs new file mode 100644 index 0000000..d445047 --- /dev/null +++ b/AmazonEncoder/AmazonEncoder.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using System.Windows.Forms; +using System.ComponentModel; + +namespace AmazonEncoder +{ + public partial class AmazonEncoder : Form + { + private readonly BackgroundWorker _worker; + 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(); + _worker.DoWork += StartEncoding; + _worker.ProgressChanged += ProgressChanged; + _worker.RunWorkerCompleted += FinishEncoding; + _worker.WorkerReportsProgress = true; + _fileDialog = new OpenFileDialog {Multiselect = true}; + progressBar.Step = 1; + } + + 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 + 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) + { + 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) + { + progressBar.PerformStep(); + label2.Text = progressBar.Value+label2.Text.Substring(label2.Text.IndexOf(' ')); + } + + 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); + 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(); + } + _worker.ReportProgress(1); + } + } + } +} + 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/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.resx b/AmazonEncoder/AmazonEncoder.resx similarity index 100% rename from AmazonEncoder/Form1.resx rename to AmazonEncoder/AmazonEncoder.resx diff --git a/AmazonEncoder/Form1.cs b/AmazonEncoder/Form1.cs deleted file mode 100644 index 932aaf9..0000000 --- a/AmazonEncoder/Form1.cs +++ /dev/null @@ -1,73 +0,0 @@ -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 -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - - // Represents the first bytes of a PNG file - 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() == System.Windows.Forms.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 - } - } - - void EncodeFiles(string[] filepaths, int total, bool delete) - { - 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 - - 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 - - // Delete old file if necessary - if (delete) - { - fi.Delete(); - } - - // Updates the form to show status - Invoke(new MethodInvoker(() => - { - done += 1; - progressBar1.Value += 1; - label2.Text = String.Format("{0} of {1} Completed", done, total); - })); - } - - // 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))); - } - } -} 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()); } } }