Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. generate rtf code of pictures/images

generate rtf code of pictures/images

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Shankar
    wrote on last edited by
    #1

    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

    H J 2 Replies Last reply
    0
    • J Jay Shankar

      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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • H Heath Stewart

        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

        J Offline
        J Offline
        Jay Shankar
        wrote on last edited by
        #3

        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.

        H 1 Reply Last reply
        0
        • J Jay Shankar

          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.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • J Jay Shankar

            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

            J Offline
            J Offline
            je_gonzalez
            wrote on last edited by
            #5

            public string ImageToRTF(Image img) { RichTextBox rtf = new RichTextBox(); Clipboard.SetDataObject(img); rtf.Paste(); return rtf.Rtf; }

            J 1 Reply Last reply
            0
            • J je_gonzalez

              public string ImageToRTF(Image img) { RichTextBox rtf = new RichTextBox(); Clipboard.SetDataObject(img); rtf.Paste(); return rtf.Rtf; }

              J Offline
              J Offline
              Jay Shankar
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups