| | |
encode-decode to base64 string
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 2,058
Reputation:
Solved Threads: 124
this is not a question, this is snippet to encode any file to base64 string and decode that encoded string to that file back. i attach the project to this post too.
Form1.cs :
Form1.Designer.cs:
Form1.cs :
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace encode_decode_Base64 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnEncode_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtInFile.Text)) { FileStream fs = new FileStream(txtInFile.Text, FileMode.Open, FileAccess.Read); byte[] filebytes = new byte[fs.Length]; fs.Read(filebytes, 0, Convert.ToInt32(fs.Length)); string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks); txtEncoded.Text = encodedData; } } private void btnDecode_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { byte[] filebytes = Convert.FromBase64String(txtEncoded.Text); FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.Write, FileShare.None); fs.Write(filebytes, 0, filebytes.Length); fs.Close(); } } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtInFile.Text = openFileDialog1.FileName; } } } }
Form1.Designer.cs:
C# Syntax (Toggle Plain Text)
namespace encode_decode_Base64 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnEncode = new System.Windows.Forms.Button(); this.btnDecode = new System.Windows.Forms.Button(); this.txtEncoded = new System.Windows.Forms.TextBox(); this.txtInFile = new System.Windows.Forms.TextBox(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.button1 = new System.Windows.Forms.Button(); this.lblBrowse = new System.Windows.Forms.Label(); this.lblEncoded = new System.Windows.Forms.Label(); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.SuspendLayout(); // // btnEncode // this.btnEncode.Location = new System.Drawing.Point(288, 87); this.btnEncode.Name = "btnEncode"; this.btnEncode.Size = new System.Drawing.Size(75, 23); this.btnEncode.TabIndex = 1; this.btnEncode.Text = "Encode"; this.btnEncode.UseVisualStyleBackColor = true; this.btnEncode.Click += new System.EventHandler(this.btnEncode_Click); // // btnDecode // this.btnDecode.Location = new System.Drawing.Point(288, 415); this.btnDecode.Name = "btnDecode"; this.btnDecode.Size = new System.Drawing.Size(75, 23); this.btnDecode.TabIndex = 2; this.btnDecode.Text = "Decode"; this.btnDecode.UseVisualStyleBackColor = true; this.btnDecode.Click += new System.EventHandler(this.btnDecode_Click); // // txtEncoded // this.txtEncoded.Location = new System.Drawing.Point(28, 155); this.txtEncoded.Multiline = true; this.txtEncoded.Name = "txtEncoded"; this.txtEncoded.Size = new System.Drawing.Size(381, 228); this.txtEncoded.TabIndex = 3; // // txtInFile // this.txtInFile.Location = new System.Drawing.Point(28, 87); this.txtInFile.Name = "txtInFile"; this.txtInFile.Size = new System.Drawing.Size(161, 20); this.txtInFile.TabIndex = 5; // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // button1 // this.button1.Location = new System.Drawing.Point(212, 85); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(43, 23); this.button1.TabIndex = 6; this.button1.Text = "..."; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // lblBrowse // this.lblBrowse.AutoSize = true; this.lblBrowse.Location = new System.Drawing.Point(35, 57); this.lblBrowse.Name = "lblBrowse"; this.lblBrowse.Size = new System.Drawing.Size(113, 13); this.lblBrowse.TabIndex = 7; this.lblBrowse.Text = "Select a file to encode"; // // lblEncoded // this.lblEncoded.AutoSize = true; this.lblEncoded.Location = new System.Drawing.Point(35, 126); this.lblEncoded.Name = "lblEncoded"; this.lblEncoded.Size = new System.Drawing.Size(105, 13); this.lblEncoded.TabIndex = 8; this.lblEncoded.Text = "Encoded file content"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(517, 684); this.Controls.Add(this.lblEncoded); this.Controls.Add(this.lblBrowse); this.Controls.Add(this.button1); this.Controls.Add(this.txtInFile); this.Controls.Add(this.txtEncoded); this.Controls.Add(this.btnDecode); this.Controls.Add(this.btnEncode); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnEncode; private System.Windows.Forms.Button btnDecode; private System.Windows.Forms.TextBox txtEncoded; private System.Windows.Forms.TextBox txtInFile; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label lblBrowse; private System.Windows.Forms.Label lblEncoded; private System.Windows.Forms.SaveFileDialog saveFileDialog1; } }
Thanks for sharing. Use the code snippits area of DaniWeb to post your code snipits - http://www.daniweb.com/code/csharp.html
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
•
•
Join Date: Jan 2008
Posts: 2,058
Reputation:
Solved Threads: 124
•
•
•
•
Thanks for sharing. Use the code snippits area of DaniWeb to post your code snipits - http://www.daniweb.com/code/csharp.html
![]() |
Similar Threads
- Passing data from forms from one page to another (JavaScript / DHTML / AJAX)
- Help with errors (Java)
- python string encoding (Python)
- Design pattern for encode/decode modules (C++)
- Passing string from txt file to byte buff[] error (C++)
- ROT-13 program (Python)
- GET! GOT! GOTCHA! I still don't Get it... (C++)
- Encoding/Decoding (C)
- help neede about converting characters (C++)
Other Threads in the C# Forum
- Previous Thread: Adding rows to a datagrid.
- Next Thread: wierd conclusion about foreach statement
Views: 1828 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net audio bitmap box broadcast button c# check checkbox class code color combobox connectionproblem control conversion csharp custom data database datagrid datagridview dataset datatable degrees display dll drawing event excel exception file flash form format forms function game gcd gdi+ graphics grid image index input install instance internet key list listbox login loop math multithreading mysql notepad office operator picturebox print procedure programming query reflection remote remoting resource round saving search sendkeys server sleep socket sql statistics string text textbox threading time timer update url validation vc++ visual webbrowser windows winforms wpf write xml






