WPF RichTextBox转化为rtf格式,以及加载,压缩

  在wpf中RichTextBox式一个富文本控件,在其中我们可以添加图片等内部控件,以及控制段落块的字体等。我们可以采用System.Windows.Markup.XamlWriter.Save(object,stream);来保存,但是例如我们的image(数据源为二进制)内部控件等,这对象无法序列化。这是我们可以采用rtf结构保存和传输,并在另一台机子加载上展现出现。只是rtf数据可能太大,此时我们可以采用ms内置的System.IO.Compression.DeflateStream 压缩压缩后在发送。

关于System.IO.Compression.DeflateStream,msdn上有一句描述如下:

此类表示 Deflate 算法,这是无损压缩和解压缩文件的行业标准算法。 它结合了 LZ77 算法和霍夫曼编码。 只能使用以前绑定的中间存储量来产生或使用数据,即使对于任意长度的、按顺序出现的输入数据流也是如此。 这种格式可以通过不涉及专利使用权的方式轻松实现。 有关更多信息,请参见 RFC 1951。" DEFLATE Compressed Data Format Specification version 1.3(DEFLATE 压缩数据格式规范版本 1.3)。"此类不能用于压缩大于 4 GB 的文件。

此类原本并不提供用来向 .zip 存档中添加文件或从 .zip 存档中提取文件的功能。(原链接

1:存储和导入rtf的代码:

ExpandedBlockStart.gif 代码
  public   static   class  RichTextBoxEx
    {
        
public   static   string  RTF( this  RichTextBox richTextBox)
        {
            
string  rtf  =   string .Empty;
            TextRange textRange 
=   new  TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            
using  (MemoryStream ms  =   new  MemoryStream())
            {
                textRange.Save(ms, System.Windows.DataFormats.Rtf);
                ms.Seek(
0 , SeekOrigin.Begin);
                StreamReader sr 
=   new  StreamReader(ms);
                rtf 
=  sr.ReadToEnd();
            }

            
return  rtf;
        }

        
public   static   void  LoadFromRTF( this  RichTextBox richTextBox,  string  rtf)
        {
            
if  ( string .IsNullOrEmpty(rtf))
            {
                
throw   new  ArgumentNullException();
            }
            TextRange textRange 
=   new  TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            
using  (MemoryStream ms  =   new  MemoryStream())
            {
                
using  (StreamWriter sw  =   new  StreamWriter(ms))
                {
                    sw.Write(rtf);
                    sw.Flush();
                    ms.Seek(
0 , SeekOrigin.Begin);
                    textRange.Load(ms, DataFormats.Rtf);
                }
            }
        }

    }

2:压缩的简单封装,转化为二进制,以及二进制转化为Base64String等::

ExpandedBlockStart.gif 代码
public   class  StringCompress
    {
        
public   static   string  Decompress( byte [] bys)
        {
            
return  Decompress(Convert.ToBase64String(bys));
        }
        
public   static   string  Decompress( string  strSource)
        {
            
return  Decompress(strSource, ( 3   *   1024   *   1024   +   256 )); // 字符串不会超过3M
        }
        
public   static   string  Decompress( string  strSource,  int  length)
        {
            
byte [] buffer  =  Convert.FromBase64String(strSource);

            System.IO.MemoryStream ms 
=   new  System.IO.MemoryStream();
            ms.Write(buffer, 
0 , buffer.Length);
            ms.Position 
=   0 ;
            System.IO.Compression.DeflateStream stream 
=   new  System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress);
            stream.Flush();

            
int  nSize  =  length;
            
byte [] decompressBuffer  =   new   byte [nSize];
            
int  nSizeIncept  =  stream.Read(decompressBuffer,  0 , nSize);
            stream.Close();

            
return  System.Text.Encoding.Unicode.GetString(decompressBuffer,  0 , nSizeIncept); // 转换为普通的字符串
        }


        
public   static   byte [] Compress( string  strSource)
        {
            
if  (strSource  ==   null )
                
throw   new  System.ArgumentException( " 字符串为空! " );

            System.Text.Encoding encoding 
=  System.Text.Encoding.Unicode;
            
byte [] buffer  =  encoding.GetBytes(strSource);

            System.IO.MemoryStream ms 
=   new  System.IO.MemoryStream();
            System.IO.Compression.DeflateStream stream 
=   new  System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress,  true );
            stream.Write(buffer, 
0 , buffer.Length);
            stream.Close();

            buffer 
=  ms.ToArray();
            ms.Close();

            
return  buffer;
            
//  return Convert.ToBase64String(buffer);  // 将压缩后的byte[]转换为Base64String
        }

    }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值