C#richtextbox显示来自RTF的图像

时间:2013-06-07 14:24:35

标签: c# .net image richtextbox rtf

为什么我的richtextbox只显示RTF代码而不显示图像? 这是rtf代码:

  

{\ PICT \ pngblip \ picw800 \ pich600 \ picwgoal6400 \ pichgoal4800 \六角   89504E470D0A1A0A0000000D494844520000032000000258080200000015141527000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC200000EC20115284A800000FFA549444154785EEC5D05601557D67E148A06E9EEFE2BDDDD1AF5D2AD20450AB4B805A22FEE21463C21EE8EBBBB3BC51DA210B4407128752FEE9284FFC89D79F35E5E42A0DD96B2F7E49B7BCF3DE75C999957EED73BF7CDD38D9D364FC1FC718CE90BC64D438C9D8E18336D7EF6C8498393B30745250484C50684C500FCC306FB8745FB8746FB8744FB05332207320645307C83103E8870446018C01B1020E0C5F00F617832FC8255780C34C07DE020774C116EBE83047C824CE0EA4D00058ABE80416E4A450F6810015D28F00F8522038AECE530D1919212B02900370E70F60E72F60A74F4F477F0F0B7771B68EBE263E3EC6DE5E465E5E839C001D15FEF61A977B7B447F4B377EB6BA7C0D6B5B78D4B6F1BE75E​​006B444F2B27400F2BC71E031C7BF477E86E89E866A9EFDACFBE2BA55DFADA7D88B0FDA0B74DA75ED69D7A5A75EC61F57E8F01EF77EFDFBE9B65FBAE88765DFBB5EB02E8FBDE87803E8C361FF469D3B977EB4E805E8056889EAD3AF66CD9B107E2FD1EEF323A00BABFD3A11BE0EDF68CAE80B7DA21B8F8EEFBDDA10AB4006D4217EDBA5A76E8DEFF833E F6BDEDBD07B80DB202B807436AED1ECCC022A19F935F776BF72EFD9D3EECE750191FA8E8ABAF1E9DFB0874EC6DF77E2FDB0E3D6CDA75B76ADBB57FEB0FFAB5FAA05FCB0FFAB6ECDCA75A40402540ADFB011A7F28589AA0F56F02E34E4D86D4AFF587966D086DBB0E68DB6D40DBEE56EDBBDBC095ECD0D3062EA902B8BC08B8CEC6B0677432867A5F1826770DA1BDCB1A7C68E9D8CDDAADAFE340F5A3C2B0F6F8D5A06D163EA508D720467FD74080A50BA29F7300A0AF937F1F473F406F8781BDF4BE809EF63E3DECBC11B65EDD6D3C01DD6C3CBB5A7B00BA58B97F38C00DF0417F5740674B1746A77ECE8C8E7D9D00AC4300C443756810DA87BEFABB04AA23313B1E180C8FC76430DD61243418B3233119830A30563906BE2CBF15FAE39956094B17867A1102FA38FAF776F4EB4517814EDF9B4F9CCF9A2EBE5B6738710DC0C2C0CB62A5C21D00B5BA58415D05D00E5C4368D0002F46371BBACE00E814E103E801B740C097D113EE0BDE1D18DE401CA4C3C0DE0E70CBFC60CC00187C1FBA897D017443E1A418962E08B8058C016E41566E41F089B5F108B6F30CB5F70AB3F70ED3FB863B0E8C74F28B72F18F720D18EC1A14E33E28D67D509C4748BC57688217A5DE00504212BC8221457886C441EA1D822E1F4058E2C0F044BF8824FFC8E480A85440E0E0B4413119217199A1F159A109D96189 391149B980A8E421512943A201A94307A70D031C3A7AE2ED566D8F9F3A935FB21B5050B2A760E79EC29D7B8DB0CB802240E93E46B179EC3F7EEAD3F8C4E46FBEFDBE64F7FE92D2FD98220E3076EE51B0F763C6AEBD0715455876EE0118224F9E3E3BCEDFFDECAE92C373A61F9E33EDE8DC69C7E601A69F9C37FD1462DA59C2E7F3A67D8998F2F5BC29DFCC9BF26D8D01C150052A42F52FA81D68EDD379D3A06568FFC4BCE9D017E0E85CE87DFA99FCAD8E5D3BC139E68D9D06500916532B6657902E1C37631160F4D4F9716943464F9CFEE9D9CF6FDFB9734F8A148DDCBA75FBE0274783A3126112EDEF1230C035106916712C7556EB69EBE1E61F396DDEF2CD05BBF3771EF855B0A3643F637BF13EC0B6A2BD120F0EBC740AC4F5BC3FE0E21BDF0286F6EED41CF091983677397C3CE043A27E60005A86F4CBA16D594CF3E6080D4DE7064EC3B406B888E0581A5AD3CD46D01A2DB3A94C6E986031C7023B844145680A5A865E2A132C803A24753C30189563190D46B03DA391981D860A750C7022D01A348E13BCEB6FCDB14C1895091482851C4BB908C8B19860C1E913C102F6E3A939E52A0916A27A8E4504AB8BB59660319060314C389631D3AA31CDA2FB08E0CF18A09F33026916902D26BB6E702382ACDC07C187D6D623C4CE2BD4986345BB0444BB06C6B80501C78AF5088EF304009D4205 52840770AF60E1F2A29469966F580270AC818263A50444A7060D4E678EA5D2ACF024A45991C9798266A50E051C3C72E2AD566D8F9D540996E0589056C7B1CCD12C5507F2119B90F4B53982253893C2AB04F61D2CDD770852D5A2D02C117FE2F4D9B1BE6E674B8A0ECF987478FA44C09119138FCD9C746CE68493332D506F9A617ECDE1F881C3C67D3D74EC3FDAF5FA7F82E759A4BBC0A7330000000049454E44AE426082}

实际上它很长,只是删了一些。

但如果格式以类似于>的格式开头{rtf ........,它显示了图像

提前致谢

2 个答案:

答案 0 :(得分:2)

假设WinForms RichTextBox,那么是的,RichTextBox的内容必须是有效的RTF文档,因此它需要以{\rtf1元素开头,并以结束括号}结束。

示例:

  

{\ rtf1 {\ pict \ pngblip \ picw800 \ pich600 ...... blah blah blah ..... 44AE426082}}

如果你的rtf glob只是一个字符串变量,那么你可以这样做:

richTextBox1.Rtf = @"{\rtf1" + picText + "}";

答案 1 :(得分:0)

要在富文本框中显示内嵌图像,您必须执行以下操作:

//A RichTextBox with an image.
private void ImageRTB()
{
    //Create a new RichTextBox.
    RichTextBox MyRTB = new RichTextBox();

    // Create a Run of plain text and image.
    Run myRun = new Run();
    myRun.Text = "Displaying text with inline image";
    Image MyImage = new Image();
    MyImage.Source = new BitmapImage(new Uri("flower.jpg",    
    UriKind.RelativeOrAbsolute));
    MyImage.Height = 50;
    MyImage.Width = 50;
    InlineUIContainer MyUI = new InlineUIContainer();
    MyUI.Child = MyImage;

    // Create a paragraph and add the paragraph to the RichTextBox.
    Paragraph myParagraph = new Paragraph();
    MyRTB.Blocks.Add(myParagraph);

    // Add the Run and image to it.
    myParagraph.Inlines.Add(myRun);
    myParagraph.Inlines.Add(MyUI);

   //Add the RichTextBox to the StackPanel.
    MySP.Children.Add(MyRTB);
}

source