I have a string of richtext characters/tokens that I would like to feed to a richtextbox in code.
string rt = #" {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+
#"{\colortbl ;\red255\green0\blue0;}"+
#"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+
#"\cf1\f1 hello\cf0\f0 \ul world\par}";
I have attempted this :
System.IO.MemoryStream strm = new System.IO.MemoryStream();
byte[] b = Encoding.ASCII.GetBytes(rt);
strm.BeginRead(b, 0, b.Length, null, null);
richTextBox1.LoadFile(strm, RichTextBoxStreamType.RichText);
it didn't work.
can anyone give me a few sugestions.
BTW the rich text comes from saving from wordpad, opening the file with notepad and using the text with in to build my string
Rich textbox has a property named Rtf. Set that property to your string value. Also, your string has an extra space as the first character. I had to remove that before I saw your Hello World.
Expanding on gbogumil's answer:
string rt = #"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Biondi;}}"+
#"{\colortbl ;\red255\green0\blue0;}"+
#"{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20\par"+
#"\cf1\f1 hello\cf0\f0 \ul world\par}";
this.richTextBox1.Rtf = rt;
Related
I have problem with RichTextBox in C#.
When I try load to RichTextBox text like "C:\Users\adasal\Desktop\raporty_handel\rpt\rtf\bruegman.rtf" from .rtf file I gettingn something like "C:_handel.rtf"
This code is write in Active Reports console.
My code:
string resoult = "C:\\Users\\adasal\\Desktop\\raporty_handel\\rpt\\rtf\\bruegman.rtf"
System.IO.FileStream rtfCreate = System.IO.File.Create(resoult);
System.Byte[] info = new System.Text.UTF8Encoding(true).GetBytes(resoult);
rtfCreate.Write(info, 0, info.Length);
rtfCreate.Close();
System.IO.FileStream streamRTF = new System.IO.FileStream(resoult,
System.IO.FileMode.Open, System.IO.FileAccess.Read);
this.RichTextBox1.Load(streamRTF, RichTextType.Rtf);
Someone can help? I want to show whole path on report.
You have to escape '\' characters which have special meaning in RTF.
For example:
public void ActiveReport_ReportStart()
{
string resoult = "C:\\Users\\adasal\\Desktop\\raporty_handel\\rpt\\rtf\\bruegman.rtf";
this.RichTextBox1.RTF = resoult.Replace("\\", "\\\\");
}
I have a text file that contains a single word but it is with language : Arabic
I want to extract it
My code is:
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt");
Console.WriteLine(text );
I have the result with unknown characters : ????
How i can fix it?
Thanks,
Setup right codepage for your text.
System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",System.Text.Encoding.GetEncoding(codepage))
May be codepage=1256 (windows-arabic).
Your code reads the text correctly into the variable text. (Debug and See)
However, dispalying arabic characters in the windows Console is another issue (Check how to solve it Here)
You can try this:
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",Encoding.Default);
Console.WriteLine(text);
Try specifying the encoding using this StreamReader constructor:
StreamReader arabic_reader = new StreamReader(filePath, System.Text.Encoding.UTF8, true);
OR
string text = System.IO.File.ReadAllText(#"C:\CINPROCESSING\nom.txt",Encoding.UTF8);
Try :
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, true);
For reference: http://msdn.microsoft.com/en-us/library/ms143457.aspx
I am writing a program which I need to read text from a file and display this on the graph once mouse hover the datapoint. My problem is while I read the data from text file and show it on graph it shows some "?" instead of actual character. (cannot post images sorry)
here is my code to read from file and attempted to change encoding.(no success) :
string myString = File.ReadAllText(#"read.txt");
Encoding enc_to = Encoding.GetEncoding("iso-8859-1");
Encoding enc_from = Encoding.UTF8;
byte[] InitialBytes =enc_from.GetBytes(myString);
byte[] FinalBytes = Encoding.Convert(enc_from, enc_to, InitialBytes);
string myMessage = enc_to.GetString(FinalBytes);
Please note that I dont want to show string as MessageBox.Show rather I want to show it as tooltip.
here is the text in read.txt file :
3 stands of 5½"
here is the how it is shown :
3 stands of 5�"
Use Encoding.Default:
string myString = File.ReadAllText(#"read.txt",Encoding.Default);
I have a hindi RTF file with content like:
कोलकाता, 11 दिसंबर पश्चिम बंगाल के बर्दवान जिले में कक्षा नौ की एक छात्रा ने फांसी लगाकर आत्महत्या कर ली।
In my console application I want to read that RTF file and Change some content programatically.
I using streamreader to read the file but when converting to string it is producing the following output:
ÚUæCþUèØ-SßæS‰Ø
×Âý Ñ Sßæ§Ù Üê •¤è ¼ßæ ÂØæü# ×æ˜æ ×ð´ ãUôÙð •¤æ ¼æßæ
ÖæðÂæÜ, vv ç¼â¢ÕÚ (¥æ§ü°°Ù°â)Ð ×ŠØ Âý¼ðàæ ×ð´ Sßæ§Ù Üê ¥æñÚ ÇðU¢»ê âð ¥Õ Ì•¤ •¤§ü Üæð»æð´ •¤è ×æñÌ ãUæð ¿é•¤è ãñU ¥õÚU ¥SÂÌæÜ ×ð´ ç¿ç•¤ˆâ•¤èØ âéçßÏæ¥ô´ •¤è •¤×è •ð¤ âæÍ-âæÍ ¼ßæ¥ô´ •ð¤ ¥Öæß •ð¤ Öè ¥æÚUæð ܻÌð ÚãðU ãñU¢Ð
SßæS‰Ø çßÖæ» Ùð ãUæÜæ¢ç•¤ ØãU ¼æßæ 畤Øæ ãñU 畤 Úæ…Ø ×ð´ ×æñâ×è ÚUæð», Sßæ§Ù Üê •ð¤ ©Â¿æÚ •ð¤ çÜ° Âý¼ðàæ •ð¤ ¥SÂÌæÜæð´ ×ð´ ¥æßàØ•¤ ¼ßæ¥æð´ •¤æ ÂØæü# ÂýÕ¢Ï ç•¤Øæ »Øæ ãñUÐ
I have tried the windows form RichTextBox to read the RTF file, but it always show Invalid File Format.
So what will be the best possible solution to read and modify rtf file in C#
StreamReader sr = new StreamReader(fpath, Encoding.Default, true);
string s1 = sr.ReadToEnd();
sr.Close();
also tried
using (System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox())
{
// Get the contents of the RTF file. Note that when it is
// stored in the string, it is encoded as UTF-16.
string s = System.IO.File.ReadAllText(fpath);
// Convert the RTF to plain text.
rtBox.Rtf = s; // error file format invalid
string plainText = rtBox.Text;
}
The RichTextBox control can load an RTF file directly, do not use StreamReader to read RTF file because it can contain a lot of Control Characters.
After loading the file to the RichTextBox, use the Text property to get the plain text of the file.
RichTextBox also has a SaveFile method to save the modified content to a file.
I need to open a .png file as a string and put it in the textbox.I trying to do that by this code:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
text1.Text = dialog.FileName;
string text = System.IO.File.ReadAllText(dialog.FileName);
text2.Text = text;
}
I need to get in my multiline textbox something like this:
‰PNG
IHDR O Ů /ç%O sRGB ®Îé gAMA ±Źüa pHYs Ă ĂÇo¨d
(IDATx^íť˝ŽŢF˛†'T°łUčĐá *ô,°'Zl˛€®b7t8—0ˇB‡ľ'(;7Tb#p$ř«Ş9ŐĹźŻŮě˙®ŻŰ{H6«ş^5ÉŤžţ0ăÉÁŰĆ#,WXš*CĆxŰ0˛aEVv¶yۨ•ť&ˇÉŘ
oFU¬5Ńć$cĽm”ÂrĄIX:čëŢ6ęałĄ)26ŔŰFKŘĽbiÚŚ
đ¶a´yŰ…éJśť}ěí“/F×XŮŇ®čëŢŕŇÎFŘ”Ň}šäL/¶ľ=ń÷ĎƦ,ÎŇ$çuq¶Młan¦Ý4)3«MĂ0®ŇŠ”™Í؆‘ś:¦jŮŰM]Śa$${eŁŻx»y;5~yĆ›˛#§i±5ÂŰŇőĹωMY
·Ň„ľ^ŕmèU` ŇDĆxŰ0Ś®8´.;ŰĽml°Âčž3š?€6gĆ’p‚+’EîłŃ 6[«ŕ
but I get only one word:
�PNG
Please, help me!
Binary data are best read with the BinaryReader. To display them in a TextBox you need to replace the 0x00 character so it won't disrupt the Text in the control.
This will replace the 0x00 character by a '.' :
using (BinaryReader br = new BinaryReader(File.Open(yourFile, FileMode.Open)))
{
var data = br.ReadChars ((int)br.BaseStream.Length);
StringBuilder sb = new StringBuilder();
foreach (char c in data)
if ((int)c > 0) sb.Append(c.ToString()); else sb.Append(".");
text2.Text = sb.ToString();
}
Edit:
Your original code will also work if you modify the final assignment like this:
text2.Text = text.Replace((char)0, '.');
Explanation: In C# a string can hold arbitrary bit patterns; but the old Winform TextBox is still the same as way back before C#, probably written in C++ and will not handle the old string termination character 0x0 correctly.
While the original problem is not so much the use of File.ReadAllText, it is well worth having the BinaryReader with its many interesting methods in your toolbox..
And the result is not totally useless - I just found that my test file has an embedded Photoshop ICC profile ;-)
not sure why you are trying to do this, but If that's what you really want you can use base64 encoded string
Read a Image file:
Bitmap loadedBitmap = Bitmap.FromFile(dialog.Filename);
Image imgFile = Image.FromFile(dialog.Filename);
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
text2.Text = base64String;
}
and when you are reading that string back, you can do the reverse and convert base64 encoded string into an image....