代码改变世界

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

2011-01-09 16:17  破狼  阅读(7145)  评论(6编辑  收藏  举报

  在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
        }

    }