This is a migrated thread and some comments may be shown as answers.

Image (and other) Questions

5 Answers 166 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Meerkat
Top achievements
Rank 1
Meerkat asked on 15 Jul 2010, 10:20 AM
Hello,
Could you please tell me how to insert an image using code. (I want to insert a logo at the top of each RichTextBox)

Can you tell me a way to save and retrieve images in RichTexBox. I fully understand it will something far from simple but any help would be appreciated. There does not seem to be anything in the documentation explaining how this might be done. I apologise if the subject is fully covered together with examples and I have just not seen it. If this is the case would you be kind enough to point out where it is.

Could I also ask how to move the cursor to the end of the document in code. (Using the keyboard, it is Ctrl+End)

Is it possilble to insert controls like textboxes etc. using code?

Many Thanks.

5 Answers, 1 is accepted

Sort by
0
Vesko
Telerik team
answered on 20 Jul 2010, 10:01 AM
Hello Meerkat,

Thank you for the question.

To insert image using code you could do the following:

ImageInline image = new ImageInline();
image.UriSource = new Uri("http://www.yoursite.com/image.jpg");
image.Extension = ".jpg";
image.Width = 100;
image.Height = 150;
  
// Insert the image at current caret position.
radRichTextBox.InsertInline(image);

There is also another way to create ImageInline - from stream. This could be achieved with the following code:
ImageInline image = new ImageInline(inputStream, new Size(100, 150), ".jpg");
  
// Insert the image at current caret position.
radRichTextBox.InsertInline(image);

If I have understand your question, you are asking for saving your document along with the inserted images and then loading it back. This could be done using the document format providers. For example you can save the document in .docx format in the following way:
IDocumentFormatProvider provider = new DocxFormatProvider();
provider.Export(radRichTextBox.Document, output);

To import it back again, you can use the following code:
radRichTextBox.Document = provider.Import(inputStream);

To move to the document's end, you could do the following:
radRichTextBox.Document.CaretPosition.MoveToLastPositionInDocument();

For more information you can look in our documentation.
I hope this helps and if you have any further questions - please feel free to ask.

Best wishes,
Vesko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Meerkat
Top achievements
Rank 1
answered on 20 Jul 2010, 02:09 PM
Hello Vesko,
Many thanks for your reply.
Your suggestion on how to move to the end of document worked fine, thankyou.

I tried hard but I could not insert an image using the following code:
When I click on http://localhost:10220/ImageFolder/Desert.jpg the image opens in a separate window so I am guessing the address is correct.
However all that gets inserted into the RichTextBox is a blank space (200x15)


ImageInline image = new ImageInline(); 
image.UriSource = new Uri("http://localhost:10220/ImageFolder/Desert.jpg"); 
image.Extension = ".jpg"; 
image.Width = 200; 
image.Height = 150;
radRichTextBox.InsertInline(image);

Regarding your suggeston on how to save and retrieve a document containg images to a database, could I ask for a little more help.
(However hard I try, I cannot think of any practical use for saving to disk so I am assuming your suggestion IS for saving to a database)

I can save and retrieve Xaml to a database using the following code but obviously images are not saved.
I tried changing XamlFormatProvider for DocxFormatProvider but I obviously did something wrong so I wonder if you might be kind enough to
expalin how to change the code.

Many thanks in anticipation.


private static string GetXamlFormDocument(RadDocument document)
 {
   IDocumentFormatProvider exporter = new XamlFormatProvider();
   string result;
   using (MemoryStream stream = new MemoryStream())
   {
     exporter.Export(document, stream);
     stream.Seek(0, SeekOrigin.Begin);
     StreamReader reader = new StreamReader(stream);
     result = reader.ReadToEnd();
   }
   return result;
 }
 private static RadDocument GetDocumentFromXaml(string xaml)
 {
   RadDocument result;
   if (!string.IsNullOrEmpty(xaml.Trim()))
   {
     IDocumentFormatProvider provider = new XamlFormatProvider();
     using (MemoryStream input = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(xaml)))
     {
       result = provider.Import(input);
     }
   }
   else
   {
     result = new RadDocument();
   }
   return result;
 }
0
Vesko
Telerik team
answered on 20 Jul 2010, 04:46 PM
Hello Meerkat,

Thanks a bunch for your question!
Actually there were 2 bugs (one of them unknown) which are now fix.
However you will be able to have the latest source code on Friday with latest internal build.

I found a temporary workaround for you which I hope will do the work. Please find a sample project attached.

The known bug was with import/export to XAML and it is with the images.
This bug is fixed by FixImages(..) method, which I have provided in the demo.

Let me know if this helps.

All the best,
Vesko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Meerkat
Top achievements
Rank 1
answered on 21 Jul 2010, 09:17 AM

Hello Vesko,
Thankyou for your sample project showing how to insert images.
Small projects like these are extremely useful as an aid to understanding.

I must admit though that I am not really clear about whether or not you have explained how to save and retrieve documents containing images to a database. Are you saying that this will possible on Friday? If so, when the bugs are fixed, I would be very grateful if you could then explain exactly how this might be done. Another small project would be very well received my myself and I suspect by others :-)

Overall, when everything is working smoothly, and adequate documentation is available, I believe the RadRichTextBox will be an excellent addition to the suite.

Regards,
Meerkat.

0
Vesko
Telerik team
answered on 21 Jul 2010, 04:32 PM
Hi Meerkat,

Thanks for your feedback - it is greatly appreciated!

To the question - when you export a document to XAML, the images are serialized in the XAML too. Our XAML format is a single file, which contains all document information needed (similar to DOCX or XPS files). And the fix for image problems will be available on Friday. You already have methods for saving/loading XAML document that use string. This means that you can store this strings to DB and later - load them back.
We have posted a sample on the forums using a DB:
http://www.telerik.com/community/forums/silverlight/general-discussions/keeping-format-of-msword.aspx

Let me know if this helps or if you have any further questions.

Kind regards,
Vesko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Meerkat
Top achievements
Rank 1
Answers by
Vesko
Telerik team
Meerkat
Top achievements
Rank 1
Share this question
or