generate rtf code of pictures/images
-
Hi, I would like to generate rtf code of the picture/image programatically without using richtext box or clip board. To be more specific I want to create .rtf file of image/pictures ? Need help in above regard...... Thanx in advance.. Regards, Jay
-
Hi, I would like to generate rtf code of the picture/image programatically without using richtext box or clip board. To be more specific I want to create .rtf file of image/pictures ? Need help in above regard...... Thanx in advance.. Regards, Jay
In the RTF specification, pictures can be encoded using hexidecimal or binary encoding. If you use binary (hexidecimal is more common), do not use CR/LF pairs to break lines (as is usual with binary files). Getting the hexidecimal value of bytes isn't hard. Just loop through the bytes and output base16 char sequences:
Image img = new Bitmap("myimage.bmp");
MemoryStream ms = new MemoryStream();
img.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
StringBuilder sb = new StringBuilder(ms.Length * 2);
byte[] buffer = new byte[4096];
int read = ms.Read(buffer, 0, buffer.Length);
while (read != 0)
{
for (int i=0; i<read; i++) sb.AppendFormat("{0:x2}", buffer[i]);
read = ms.Read(buffer, 0, buffer.Length);
}
string hex = sb.ToString();It's not exactly the most efficient, but you should get the idea. There are several sections require to embed a pictures in RTF. Read more about embedding pictures in the RTF specification at http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec_16.asp?FRAME=true#rtfspec_24[^].
Microsoft MVP, Visual C# My Articles
-
In the RTF specification, pictures can be encoded using hexidecimal or binary encoding. If you use binary (hexidecimal is more common), do not use CR/LF pairs to break lines (as is usual with binary files). Getting the hexidecimal value of bytes isn't hard. Just loop through the bytes and output base16 char sequences:
Image img = new Bitmap("myimage.bmp");
MemoryStream ms = new MemoryStream();
img.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
StringBuilder sb = new StringBuilder(ms.Length * 2);
byte[] buffer = new byte[4096];
int read = ms.Read(buffer, 0, buffer.Length);
while (read != 0)
{
for (int i=0; i<read; i++) sb.AppendFormat("{0:x2}", buffer[i]);
read = ms.Read(buffer, 0, buffer.Length);
}
string hex = sb.ToString();It's not exactly the most efficient, but you should get the idea. There are several sections require to embed a pictures in RTF. Read more about embedding pictures in the RTF specification at http://msdn.microsoft.com/library/en-us/dnrtfspec/html/rtfspec_16.asp?FRAME=true#rtfspec_24[^].
Microsoft MVP, Visual C# My Articles
Hi Steward, Thanks a lot for the solution. I have modified the code since there was some compilation error as below
//Creating a file and writing string strfileName = "PicInHex.rtf"; FileStream MyStream = new FileStream("c:\\" + strfileName, FileMode.Create); System.IO.StreamWriter MyWriter = new System.IO.StreamWrite(MyStream,System.Text.Encoding.Default); // Image img = new Bitmap("myimage.bmp"); MemoryStream ms = new MemoryStream(); // //Added System.Drawing.Imaging.ImageFormat.Bmp in the line below(there was compilation err) // img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); // //Added Convert.ToInt32 in the below line(there was compilation err) // StringBuilder sb = new StringBuilder(Convert.ToInt32(ms.Length * 2)); byte[] buffer = new byte[4096]; int read = ms.Read(buffer, 0, buffer.Length); while (read != 0){ //<< ...is less than sign, // modified since it interfares with the HTML tags for (int i=0; i< **I want to get the hex code of the image using the above code then prefix and suffix image rtf syntax in it to get rtf code file** when I try to open the "PicInHex.rtf" (created after executing the above code)using notepad to view the hex code. the NotePad application crashes. _**Will you please commnet whether 1. can I open and view the code using notepad (the bmp is of A4 size) 2. Am I correct in approaching the solution?**_ Let me thank you once again for your co-operation and solution. Regards, Jay.
-
Hi Steward, Thanks a lot for the solution. I have modified the code since there was some compilation error as below
//Creating a file and writing string strfileName = "PicInHex.rtf"; FileStream MyStream = new FileStream("c:\\" + strfileName, FileMode.Create); System.IO.StreamWriter MyWriter = new System.IO.StreamWrite(MyStream,System.Text.Encoding.Default); // Image img = new Bitmap("myimage.bmp"); MemoryStream ms = new MemoryStream(); // //Added System.Drawing.Imaging.ImageFormat.Bmp in the line below(there was compilation err) // img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); // //Added Convert.ToInt32 in the below line(there was compilation err) // StringBuilder sb = new StringBuilder(Convert.ToInt32(ms.Length * 2)); byte[] buffer = new byte[4096]; int read = ms.Read(buffer, 0, buffer.Length); while (read != 0){ //<< ...is less than sign, // modified since it interfares with the HTML tags for (int i=0; i< **I want to get the hex code of the image using the above code then prefix and suffix image rtf syntax in it to get rtf code file** when I try to open the "PicInHex.rtf" (created after executing the above code)using notepad to view the hex code. the NotePad application crashes. _**Will you please commnet whether 1. can I open and view the code using notepad (the bmp is of A4 size) 2. Am I correct in approaching the solution?**_ Let me thank you once again for your co-operation and solution. Regards, Jay.
It was sample code - it wasn't meant to be complete, but to serve as an example. Notepad is a vanilla (i.e., plain) text editor so it won't view anything for the RTF "code" itself. The image will appear as what it appears in the actual RTF: hex. So long as your hex-encoding the image and using the right RTF codes, then there really is no other way to approach this without using some third-party library or some RTF control like the
RichTextBox
.Microsoft MVP, Visual C# My Articles
-
Hi, I would like to generate rtf code of the picture/image programatically without using richtext box or clip board. To be more specific I want to create .rtf file of image/pictures ? Need help in above regard...... Thanx in advance.. Regards, Jay
public string ImageToRTF(Image img) { RichTextBox rtf = new RichTextBox(); Clipboard.SetDataObject(img); rtf.Paste(); return rtf.Rtf; }
-
public string ImageToRTF(Image img) { RichTextBox rtf = new RichTextBox(); Clipboard.SetDataObject(img); rtf.Paste(); return rtf.Rtf; }
Dear Heath Stewart and Gonzalez, thanks a lot for the solution and guidance, it solves the purpose. I am pasting my complete Demo code.This can be helpful to another developers
//For running the sample code paste the following in button click event private void button1_Click(object sender, System.EventArgs e) { string strImageToRTF ="";// string to hold rtf code //create the file where rtf code has to be written string strfileName = "ImageInRTFfile.rtf"; FileStream MyStream = new FileStream("c:\\" + strfileName, FileMode.Create); System.IO.StreamWriter MyWriter =new System.IO.StreamWriter(MyStream,System.Text.Encoding.Default); // //Prepare image object from image file Image img = new Bitmap(@"c:\myimage.bmp"); // //get the RTF string from the method ImageToRTF strImageToRTF = this.ImageToRTF(img); // //write the generated RTF string into the file MyWriter.Write(strImageToRTF); // //close Stream Writer and File Stream MyWriter.Close(); MyStream.Close(); // } // // // Method to get the rtf code string from the image object private string ImageToRTF(Image img) { RichTextBox rtf = new RichTextBox(); Clipboard.SetDataObject(img); rtf.Paste(); return rtf.Rtf; } // //
Thanks and Regards, Jay.