zoukankan      html  css  js  c++  java
  • 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的代码:

    代码
     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等::

    代码
    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
            }

        }

     


    作者:破  狼
    出处:http://www.cnblogs.com/whitewolf/
    本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼

  • 相关阅读:
    JavaScript(一)
    数据库(二)
    分布式系统框架Spring+Redis+SSO视频课程
    3、尚硅谷_SSM高级整合_使用ajax操作实现删除的功能
    3、尚硅谷_SSM高级整合_使用ajax操作实现修改员工的功能
    3、尚硅谷_SSM高级整合_使用ajax操作实现增加员工的功能
    2、尚硅谷_SSM高级整合_使用ajax操作实现页面的查询功能
    2、尚硅谷_SSM高级整合_创建Maven项目.avi
    尚硅谷maven视频教程笔记
    mybatis视频教程2-动态参数
  • 原文地址:https://www.cnblogs.com/whitewolf/p/1931290.html
Copyright © 2011-2022 走看看