losange
losange

Reputation: 335

Richtextbox not displaying image

Edit: Thank to comments I noticed the root cause is setting ReadOnly property to true

I am creating a winform with a richtextbox (using classname RICHEDIT50W - see custom class below)

If I insert rich text, tables and pictures it's displayed as expected. I can save the RTF content and load it back.

But if I set the property readonly to true the image is not displayed (tables and other rich content are displayed). Any thought why ?

I am doing just this to load RTF data myTextBox.ReadOnly = true; myTextBox.RTF = variableConteningRtfData;

My customrichtextbox implementation has been found on stackoverflow and is there to better manage RTF specification.

public class custom_RichTextBox : RichTextBox
{
    public custom_RichTextBox() : base()
    {

    }

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr LoadLibrary(string lpFileName);

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams param = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
            {
                param.ClassName = "RICHEDIT50W";
            }
            return param;
        }
    }
}

Upvotes: 1

Views: 3877

Answers (3)

user13931961
user13931961

Reputation: 11

I have found that you can load the rtf file and the richtextbox as readonly = false and then in the load of the form change it to readonly= true and the images remain and the scrollbars work and the data cannot be deleted or changed.

Upvotes: 1

M_A_
M_A_

Reputation: 11

I found the work around the possible cause.

The RichTextBox inside .NET does not support the version of the Rich Text file that you stored. You also probably used the WordPad on your Windows.

The trick is that you place a RichTextBox control on a form and let the RichTextBox load the RTF file you intend to load like:

richTextBox1.LoadFile("Your File Path", RichTextBoxStreamType.RichText);

Run your application and let the RTF file get loaded on that RichTextBox. Then obviously at this time it does NOT load the images.

Open the original RTF file using WordPad then capture images using Windows Snipping Tool from that RTF file.

On RichTextBox being run in your application, paste the images that you captured using Snipping Tool. In your Application that you have RichTextBox save that new RichTextBox content that you inserted images.

Now at this time, the new RTF file you saved from your own application, is compatible in the RichTextBox in .NET.

You can load this new file in the RichTextBox like:

richTextBox1.LoadFile("Your File Path", RichTextBoxStreamType.RichText);

At this time you will see the images together with other RTF content. Hope this helps. It took me a while to come up with this solution.

Upvotes: 0

losange
losange

Reputation: 335

Finding the root cause give an easy answer: setting richtextbox to not readonly before loading and setting it back to readonly after. this also solves similar question here: ReadOnly content of RichTextBox doesn't show images

Upvotes: 5

Related Questions