Impossible. From your earlier code example you showed us this:
byte[] myFile = Encoding.UTF8.GetBytes(richTextBox1.Text);
richTextBox1.
Text returns the unformatting string of words in the RTF textbox. Now richTextBox1.
Rtf will give you the formatted rich text. It is impossible that you saw the formatted rich text using the code provided earlier, something else is wrong.
Now this is how you can save an RTF control to a file:
private void button2_Click(object sender, EventArgs e)
{
//This works OK
const string fName = @"C:\rtf.rtf";
if (System.IO.File.Exists(fName))
System.IO.File.Delete(fName);
System.IO.File.WriteAllBytes(fName, Encoding.UTF8.GetBytes(richTextBox1.Rtf));
System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
}
And using this SQL table:
IF OBJECT_ID('RTFStore', 'U') IS NOT NULL DROP TABLE RTFStore
CREATE TABLE RTFStore
(
RecordId INT identity(1000, 1) PRIMARY KEY,
RTF VARBINARY(max)
)
You can store/retrieve RTF to a database using:
private void button3_Click(object sender, EventArgs e)
{
const string connStr = "Data Source=apex2006sql;Initial Catalog=Scott;Integrated Security=True;";
int rtfRecordId;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
const string insQuery = "Insert Into RTFStore (RTF) Values (@RTF) Select CAST(SCOPE_IDENTITY() as int) As Result";
using (SqlCommand cmd = new SqlCommand(insQuery, conn))
{
cmd.Parameters.Add(new SqlParameter("@RTF", SqlDbType.VarBinary)).Value = System.Text.ASCIIEncoding.UTF8.GetBytes(richTextBox1.Rtf);
rtfRecordId = Convert.ToInt32(cmd.ExecuteScalar());
}
}
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
const string selQuery = "Select RTF From RTFStore Where RecordId = @RecordId";
using (SqlCommand cmd = new SqlCommand(selQuery, conn))
{
cmd.Parameters.Add(new SqlParameter("@RecordId", SqlDbType.Int)).Value = rtfRecordId;
object oBuffer = cmd.ExecuteScalar();
if (oBuffer != DBNull.Value)
{
byte[] buffer = (byte[])oBuffer;
const string fName = @"C:\rtf2.rtf";
File.WriteAllBytes(fName, buffer);
System.Diagnostics.Process.Start("wordpad", "\"" + fName + "\"");
}
}
}
}
I'm not sure what is wrong with your original code since you haven't posted the complete sample but give this a shot.