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

Can the RichTextBox be used on the server?

5 Answers 78 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Lee Saunders
Top achievements
Rank 1
Lee Saunders asked on 09 Jan 2012, 06:18 PM
My company is currently looking at the possibility of combining the use of the Silverlight and the WPF RichTextBoxes (RTB).

On the client side we would have the Silverlight RTB.  It would send its XAML to the server to be saved into the database.

Then from a service the XAML would be loaded into a in-memory WPF RTB and then an image of the content would be generated.

My Question is, can the WPF RTB be used in a service that does not have an interface?  And if it can, Will I be able to create a RenderTargetBitmap of its visual?

5 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 12 Jan 2012, 09:08 AM
Hi Lee,
You can use RadRichTextBox in a service, but you should set thread apartment state to STA:
Thread thread = new Thread(() =>
{               
    RadDocument document = new XamlFormatProvider().Import(xaml);
    // do something
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
RadRichTextBox uses virtualization, so only visible parts of the UI are created from the document model and the RenderTargetBitmap approach won't work with larger documents, expanding beyond  the view port.

Don't hesitate to contact us if you have further questions.

Regards,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Lee Saunders
Top achievements
Rank 1
answered on 12 Jan 2012, 05:30 PM
Thanks!

This would only be used with short messages and the view window will be the "border" so there would be nothing beyond the view port.
0
David Hatfield
Top achievements
Rank 1
answered on 04 May 2012, 05:03 PM
Hello,

Any chance I could get a working snippet for this functionality?  Here is what I have been trying, which results in an empty png file of the correct dimensions.

private const string RtfContent = @"{\rtf1\ansi\uc1\pard Test \ul RTF\ulnone  Text\par}";
 
static void Main(string[] args)
{
    var thread = new Thread(() =>
    {
 
        var doc = new RtfFormatProvider().Import(RtfContent);
        doc.MeasureAndArrangeInDefaultSize();
 
        var width = doc.DesiredSize.Width;
        var height = doc.DesiredSize.Height;
        var vrtb = new RadRichTextBox { Document = doc, Width = width, Height = height};
 
        vrtb.ApplyTemplate();
        vrtb.Measure(new Size(width, height));
        vrtb.Arrange(new Rect(0, 0, width, height));
        vrtb.UpdateLayout();
 
        var renderTargetBitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);
        renderTargetBitmap.Render(vrtb);
 
        var encoder = new PngBitmapEncoder();
        using (var fs = new FileStream(@"C:\Temp\test.png", FileMode.Create))
        {
            encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
            encoder.Save(fs);
        }
    });
 
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

Thank You.
0
Iva Toteva
Telerik team
answered on 08 May 2012, 05:50 PM
Hi David,

It is not possible to take a snapshot of RadRichTextBox on the server. RadRichTextBox has to be added to the visual tree in order to create a bitmap out of it. This may be the behavior of BitmapSource by design, as the same code does not work for a TextBox either.

The functionality that is available on the server is to import a document from any of the supported file formats, modify it programmatically and export it. What you can do is to create the images on the client (in the Silverlight project) and only then send it to the server.  

I hope this answers your question.

Regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
David Hatfield
Top achievements
Rank 1
answered on 08 May 2012, 05:59 PM
Fair enough Iva.   Thanks for the prompt response.

David
Tags
RichTextBox
Asked by
Lee Saunders
Top achievements
Rank 1
Answers by
Boby
Telerik team
Lee Saunders
Top achievements
Rank 1
David Hatfield
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or