Beispiel #1
0
		//text to base64
		private void text2base64button_Click(object sender, EventArgs e)
		{
			string textStr="";
			if (textBox.Text.Length != 0 || encodingchoiceBox1.SelectedIndex ==5)
			{
				string filePath="";
				byte[] encodedDataBytes = new byte[5];
				//get encoded data bytes array from text box or file
				switch (encodingchoiceBox1.SelectedIndex)
				{
					case 0: //defualt
						encodedDataBytes = Encoding .Default .GetBytes(textBox.Text);
						break;
					case 1: //ASCII
						encodedDataBytes = Encoding .ASCII .GetBytes(textBox.Text);
						break;
					case 2: //UTF-8
						encodedDataBytes = Encoding .UTF8 .GetBytes(textBox.Text);
						break;
					case 3: //Unicode
						encodedDataBytes = Encoding .Unicode .GetBytes(textBox.Text); 
						break;
					case 4: //Unicode-BigEndian
						encodedDataBytes = Encoding .BigEndianUnicode .GetBytes(textBox.Text);
						break;
					case 5: //Binary
						try
						{
							FileStream fs = new FileStream(filePath = Regex.Replace(textFilePathBox.Text, "(\"|\')", ""), FileMode.Open);
							encodedDataBytes = new byte[fs.Length];
							fs.Read(encodedDataBytes, 0, (int)fs.Length);
							fs.Close();
						}
						catch
						{
							MessageBox.Show(Properties.Resources.MainForm_invalidPathAlert);
							return;
						}
						break;
					case 6: //Code page
						int codePage = 0;
						try
						{
							codePage = Convert.ToInt32(codePageTextBox.Text);
#if DEBUG_CODEPAGE
							MessageBox.Show(codePage.ToString());
#endif
							encodedDataBytes = Encoding.GetEncoding(codePage).GetBytes(textBox.Text);
						}
						catch
						{
							CodePageHelp codePageHelpBox = new CodePageHelp();
							codePageHelpBox.OpenHelpFormHandler += new CodePageHelp.openHelpFormHandler(helpHelpToolStripMenuItem_Click);
							codePageHelpBox.ShowDialog();
							return;
						}
						break;
					default: //??
						encodedDataBytes = Encoding .Default .GetBytes(textBox.Text);
						break;
				}
				//output base64 string to base64 text box or file
				if (base64FileModeCheckBox.Checked == true)
				{
					string toWriteFilePath = Regex.Replace(base64FilePathBox.Text, "(\"|\')", "");
					try
					{
						byte[] textBytes = new byte[5];
						if (File.Exists(toWriteFilePath))
						{
							if (MessageBox.Show(Properties.Resources.MainForm_fileExistedAlert, Properties.Resources.MainForm_fileExistedAlertTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
							{
								//if file exists, clear it before write
								FileStream fs = new FileStream(toWriteFilePath, FileMode.Truncate);
								textStr = Convert.ToBase64String(encodedDataBytes);
								if (filePath.Length > 0) textStr = ASAddStyle(textStr, imageBase64Style, filePath);
								textBytes = Encoding.ASCII.GetBytes(textStr);
								fs.Write(textBytes, 0, textBytes.Length);
								fs.Close();
							}
							else
							{
								//fakeProgressBar.Visible = false; 
								return;
							}
						}
						else
						{
							FileStream fs = new FileStream(toWriteFilePath, FileMode.CreateNew);
							textStr = Convert.ToBase64String(encodedDataBytes);
							//write to with ASCII encoding, not support UTF-8 or other format files
							if (filePath.Length > 0) textStr = ASAddStyle(textStr, imageBase64Style, filePath);
							textBytes = Encoding.ASCII.GetBytes(textStr);
							fs.Write(textBytes, 0, textBytes.Length);
							fs.Close();
						}
					}
					catch
					{
						MessageBox.Show(Properties.Resources.MainForm_invalidPathAlert);
						return;
					}
				}
				else
				{
					textStr = Convert.ToBase64String(encodedDataBytes);
					if (sender == text2base64button) isConverting = true;
					if (filePath.Length > 0)
					{
						textStr = ASAddStyle(textStr, imageBase64Style, filePath);
					}
					base64Box.Text = textStr;
					if (sender == text2base64button) isConverting = false;
					ASAutoCopy(labelBase64Copied, base64Box, sender == text2base64button);
				}
			}
		}
Beispiel #2
0
		private void Box_DragDrop(object sender, DragEventArgs e)
		{
			string filePath = "";
			if (e.Data.GetDataPresent(DataFormats.Text))
			{
				((RichTextBox)sender).Text = e.Data.GetData(DataFormats.Text).ToString();
			}
			else  //file drop
			{
				//show the content of file in the text/base64 box
				//or turn to binary mode for image files
				filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0].ToString();
				if (!IsImageFile_DragedDrop(sender,filePath))
				{
					switch (encodingchoiceBox1.SelectedIndex)
					{
						case 0: //defualt
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.Default);
							break;
						case 1: //ASCII
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.ASCII);
							break;
						case 2: //UTF-8
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.UTF8);
							break;
						case 3: //Unicode
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.Unicode);
							break;
						case 4: //Unicode-BigEndian
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.BigEndianUnicode);
							break;
						case 6: //Code page
							int codePage = 0;
							try
							{
								codePage = Convert.ToInt32(codePageTextBox.Text);
#if DEBUG_CODEPAGE
								MessageBox.Show(codePage.ToString());
#endif
								((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.GetEncoding(codePage));
							}
							catch
							{
								CodePageHelp codePageHelpBox = new CodePageHelp();
								codePageHelpBox.OpenHelpFormHandler += new CodePageHelp.openHelpFormHandler(helpHelpToolStripMenuItem_Click);
								codePageHelpBox.ShowDialog();
								return;
							}
							break;
						default: //??
							((RichTextBox)sender).Text = File.ReadAllText(filePath, Encoding.Default);
							break;
					}
				}
			}
		}
Beispiel #3
0
		//base64 to text
		private void base642textbutton_Click(object sender, EventArgs e)
		{	
			string base64Str = base64Box.Text;
			byte[] decodedDataBytes,base64Bytes = new byte[5];
			if (base64Str.Length != 0 || base64FileModeCheckBox.Checked == true)
			{
				try
				{
					//get decoded data bytes from base64 text box or file
					if (base64FileModeCheckBox.Checked == true)
					{
						try
						{
							FileStream fs = new FileStream(Regex.Replace(base64FilePathBox.Text, "(\"|\')", ""), FileMode.Open);
							base64Bytes = new byte[fs.Length];
							fs.Read(base64Bytes, 0, (int)fs.Length);
							fs.Close();
						}
						catch
						{
							MessageBox.Show(Properties.Resources.MainForm_invalidPathAlert);
							return;
						}
						try
						{
							//read from file with ASCII encoding, not support UTF-8 or other format files
							base64Str = Encoding.ASCII.GetString(base64Bytes);
							decodedDataBytes = Convert.FromBase64String(base64Str);
						}
						catch
						{
							MessageBox.Show(Properties.Resources.MainForm_invalidBase64Alert);
							return;
						}
					}
					else
					{
#if DEBUG_BASE64TOFILE
						MessageBox.Show(@"length:" + base64Str.Length.ToString());
#endif
						decodedDataBytes = Convert.FromBase64String(base64Str);
					}
					//output decoded text to text box or file
					switch (encodingchoiceBox1.SelectedIndex)
					{
						case 0: //defualt
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .Default .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender==base642textbutton);
							break;
						case 1: //ASCII
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .ASCII .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender==base642textbutton);
							break;
						case 2: //UTF-8
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .UTF8 .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender==base642textbutton);
							break;
						case 3: //Unicode
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .Unicode .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender==base642textbutton);
							break;
						case 4: //Unicode-BigEndian
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .BigEndianUnicode .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender==base642textbutton);
							break;
						case 5: //Binary
							string toWriteFilePath = Regex.Replace(textFilePathBox.Text, "(\"|\')", "");
							try
							{
								if (File.Exists(toWriteFilePath))
								{
									if (MessageBox.Show(Properties.Resources.MainForm_fileExistedAlert, Properties.Resources.MainForm_fileExistedAlertTitle, MessageBoxButtons.YesNo) == DialogResult.Yes)
									{
										FileStream fs = new FileStream(toWriteFilePath, FileMode.Truncate, FileAccess.Write);
										fs.Write(decodedDataBytes, 0, decodedDataBytes.Length);
										fs.Close();
									}
									else
									{
										return;
									}
								}
								else
								{
									FileStream fs = new FileStream(toWriteFilePath, FileMode.CreateNew);
									fs.Write(decodedDataBytes, 0, decodedDataBytes.Length);
									fs.Close();
								}
							}
							catch
							{
								MessageBox.Show(Properties.Resources.MainForm_invalidPathAlert);
								return;
							}
							break;
						case 6: //Code page
							int codePage = 0;
							try
							{
								codePage = Convert.ToInt32(codePageTextBox.Text);
#if DEBUG_CODEPAGE
								MessageBox.Show(codePage.ToString());
#endif
								if (sender == base642textbutton) isConverting = true;
								textBox.Text = Encoding.GetEncoding(codePage).GetString(decodedDataBytes);
								if (sender == base642textbutton) isConverting = false;
								ASAutoCopy(labelTextCopied, textBox, sender == base642textbutton);
							}
							catch
							{
								CodePageHelp codePageHelpBox = new CodePageHelp();
								codePageHelpBox.OpenHelpFormHandler += new CodePageHelp.openHelpFormHandler(helpHelpToolStripMenuItem_Click);
								codePageHelpBox.ShowDialog();
								//fakeProgressBar.Visible = false; 
								return;
							}
							break;
						default: //??
							if (sender == base642textbutton) isConverting = true;
							textBox.Text = Encoding .Default .GetString(decodedDataBytes);
							if (sender == base642textbutton) isConverting = false;
							ASAutoCopy(labelTextCopied, textBox, sender == base642textbutton);
							break;
					}
				}
				catch
				{
					MessageBox.Show(Properties.Resources.MainForm_invalidBase64Alert);
					return;
				}
			}
		}