encode-decode to base64 string

Thread Solved

Join Date: Jan 2008
Posts: 2,058
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 124
Featured Poster
serkan sendur serkan sendur is offline Offline
Banned

encode-decode to base64 string

 
0
  #1
Jun 3rd, 2009
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 :
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9.  
  10. namespace encode_decode_Base64
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void btnEncode_Click(object sender, EventArgs e)
  19. {
  20. if (!string.IsNullOrEmpty(txtInFile.Text))
  21. {
  22. FileStream fs = new FileStream(txtInFile.Text,
  23. FileMode.Open,
  24. FileAccess.Read);
  25. byte[] filebytes = new byte[fs.Length];
  26. fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
  27. string encodedData =
  28. Convert.ToBase64String(filebytes,
  29. Base64FormattingOptions.InsertLineBreaks);
  30. txtEncoded.Text = encodedData;
  31. }
  32.  
  33. }
  34.  
  35. private void btnDecode_Click(object sender, EventArgs e)
  36. {
  37. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  38. {
  39.  
  40. byte[] filebytes = Convert.FromBase64String(txtEncoded.Text);
  41. FileStream fs = new FileStream(saveFileDialog1.FileName,
  42. FileMode.CreateNew,
  43. FileAccess.Write,
  44. FileShare.None);
  45. fs.Write(filebytes, 0, filebytes.Length);
  46. fs.Close();
  47.  
  48. }
  49.  
  50. }
  51.  
  52. private void button1_Click(object sender, EventArgs e)
  53. {
  54. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  55. {
  56. txtInFile.Text = openFileDialog1.FileName;
  57. }
  58. }
  59.  
  60.  
  61. }
  62. }

Form1.Designer.cs:
  1. namespace encode_decode_Base64
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.btnEncode = new System.Windows.Forms.Button();
  32. this.btnDecode = new System.Windows.Forms.Button();
  33. this.txtEncoded = new System.Windows.Forms.TextBox();
  34. this.txtInFile = new System.Windows.Forms.TextBox();
  35. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  36. this.button1 = new System.Windows.Forms.Button();
  37. this.lblBrowse = new System.Windows.Forms.Label();
  38. this.lblEncoded = new System.Windows.Forms.Label();
  39. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  40. this.SuspendLayout();
  41. //
  42. // btnEncode
  43. //
  44. this.btnEncode.Location = new System.Drawing.Point(288, 87);
  45. this.btnEncode.Name = "btnEncode";
  46. this.btnEncode.Size = new System.Drawing.Size(75, 23);
  47. this.btnEncode.TabIndex = 1;
  48. this.btnEncode.Text = "Encode";
  49. this.btnEncode.UseVisualStyleBackColor = true;
  50. this.btnEncode.Click += new System.EventHandler(this.btnEncode_Click);
  51. //
  52. // btnDecode
  53. //
  54. this.btnDecode.Location = new System.Drawing.Point(288, 415);
  55. this.btnDecode.Name = "btnDecode";
  56. this.btnDecode.Size = new System.Drawing.Size(75, 23);
  57. this.btnDecode.TabIndex = 2;
  58. this.btnDecode.Text = "Decode";
  59. this.btnDecode.UseVisualStyleBackColor = true;
  60. this.btnDecode.Click += new System.EventHandler(this.btnDecode_Click);
  61. //
  62. // txtEncoded
  63. //
  64. this.txtEncoded.Location = new System.Drawing.Point(28, 155);
  65. this.txtEncoded.Multiline = true;
  66. this.txtEncoded.Name = "txtEncoded";
  67. this.txtEncoded.Size = new System.Drawing.Size(381, 228);
  68. this.txtEncoded.TabIndex = 3;
  69. //
  70. // txtInFile
  71. //
  72. this.txtInFile.Location = new System.Drawing.Point(28, 87);
  73. this.txtInFile.Name = "txtInFile";
  74. this.txtInFile.Size = new System.Drawing.Size(161, 20);
  75. this.txtInFile.TabIndex = 5;
  76. //
  77. // openFileDialog1
  78. //
  79. this.openFileDialog1.FileName = "openFileDialog1";
  80. //
  81. // button1
  82. //
  83. this.button1.Location = new System.Drawing.Point(212, 85);
  84. this.button1.Name = "button1";
  85. this.button1.Size = new System.Drawing.Size(43, 23);
  86. this.button1.TabIndex = 6;
  87. this.button1.Text = "...";
  88. this.button1.UseVisualStyleBackColor = true;
  89. this.button1.Click += new System.EventHandler(this.button1_Click);
  90. //
  91. // lblBrowse
  92. //
  93. this.lblBrowse.AutoSize = true;
  94. this.lblBrowse.Location = new System.Drawing.Point(35, 57);
  95. this.lblBrowse.Name = "lblBrowse";
  96. this.lblBrowse.Size = new System.Drawing.Size(113, 13);
  97. this.lblBrowse.TabIndex = 7;
  98. this.lblBrowse.Text = "Select a file to encode";
  99. //
  100. // lblEncoded
  101. //
  102. this.lblEncoded.AutoSize = true;
  103. this.lblEncoded.Location = new System.Drawing.Point(35, 126);
  104. this.lblEncoded.Name = "lblEncoded";
  105. this.lblEncoded.Size = new System.Drawing.Size(105, 13);
  106. this.lblEncoded.TabIndex = 8;
  107. this.lblEncoded.Text = "Encoded file content";
  108. //
  109. // Form1
  110. //
  111. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  112. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  113. this.ClientSize = new System.Drawing.Size(517, 684);
  114. this.Controls.Add(this.lblEncoded);
  115. this.Controls.Add(this.lblBrowse);
  116. this.Controls.Add(this.button1);
  117. this.Controls.Add(this.txtInFile);
  118. this.Controls.Add(this.txtEncoded);
  119. this.Controls.Add(this.btnDecode);
  120. this.Controls.Add(this.btnEncode);
  121. this.Name = "Form1";
  122. this.Text = "Form1";
  123. this.ResumeLayout(false);
  124. this.PerformLayout();
  125.  
  126. }
  127.  
  128. #endregion
  129.  
  130. private System.Windows.Forms.Button btnEncode;
  131. private System.Windows.Forms.Button btnDecode;
  132. private System.Windows.Forms.TextBox txtEncoded;
  133. private System.Windows.Forms.TextBox txtInFile;
  134. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  135. private System.Windows.Forms.Button button1;
  136. private System.Windows.Forms.Label lblBrowse;
  137. private System.Windows.Forms.Label lblEncoded;
  138. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  139. }
  140. }
Attached Files
File Type: zip encode-decode-Base64.zip (36.5 KB, 13 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 26
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: encode-decode to base64 string

 
0
  #2
Jun 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,058
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 124
Featured Poster
serkan sendur serkan sendur is offline Offline
Banned

Re: encode-decode to base64 string

 
0
  #3
Jun 4th, 2009
Originally Posted by Tekmaven View Post
Thanks for sharing. Use the code snippits area of DaniWeb to post your code snipits - http://www.daniweb.com/code/csharp.html
it does not have attachment facility.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1828 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC