把图片转换成二进制流存储时,几乎文本的大小与图片的大小一样大小.怎么保存文本更小一点?还有我用二进制存,打开的时候总可以看到乱码.怎么样才可以文本里就只二进制的数据?

解决方案 »

  1.   

    图片转换成Base64格式保存在xml中
    节省空间
      

  2.   

    首先编写实现Base64编码与其它编码转换的类,类名为Base64Provider,代码如下:
    using System;
    using System.Text;/// <summary>
    /// 实现Base64编码与其它编码转换的类
    /// </summary>
    public class Base64Provider
    {
    private Base64Provider()
    {
    }
        /// <summary>
        /// 将其它编码的字符串转换成Base64编码的字符串
        /// </summary>
        /// <param name="source">要转换的字符串</param>
        /// <returns></returns>
        public static string EncodeBase64String(string source)
        {
            //如果字符串为空或者长度为0则抛出异常
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException("source", "不能为空。");
            }
            else
            {
                //将字符串转换成UTF-8编码的字节数组
                byte[] buffer = Encoding.UTF8.GetBytes(source);
                //将UTF-8编码的字节数组转换成Base64编码的字符串
                string result = Convert.ToBase64String(buffer);
                return result;
            }
        }
        /// <summary>
        /// 将Base64编码的字符串转换成其它编码的字符串
        /// </summary>
        /// <param name="result">要转换的Base64编码的字符串</param>
        /// <returns></returns>
        public static string DecodeBase64String(string result)
        {
            //如果字符串为空或者长度为0则抛出异常
            if (string.IsNullOrEmpty(result))
            {
                throw new ArgumentNullException("result", "不能为空。");
            }
            else
            {
                //将字符串转换成Base64编码的字节数组
                byte[] buffer = Convert.FromBase64String(result);
                //将字节数组转换成UTF-8编码的字符串
                string source = Encoding.UTF8.GetString(buffer);
                return source;
            }
        }
    }
      

  3.   

    这里提供的是字符串形式的,可以稍微改造提供对字节数组的Base64编码和解码的。实现很简单,你自己照葫芦画瓢吧。
      

  4.   

    http://www.cnblogs.com/dreign/archive/2006/09/14/503979.aspxprivate   void   button1_Click(object   sender,   System.EventArgs   e)   
      {   
      FileStream   fs   =     null;   
      BinaryReader   br   =   null;   
      StreamWriter   sw     =   null;   
      try   
      {    
      fs     =   new   FileStream("a.bmp",FileMode.Open,FileAccess.Read);   
      br   =   new   BinaryReader(fs);   
      sw   =   new   StreamWriter("bb.txt");   
      int   length   =   (int)fs.Length;   
      while(length   >   0)   
      {   
      byte   tempByte   =   br.ReadByte();   
      int   tempInt   =Convert.ToInt32(tempByte);   
      string   tempStr   =   Convert.ToString(tempInt,16);   
              sw.WriteLine(tempStr);   
      length--;   
      }     }   
      catch(Exception   exce)   
      {   
      MessageBox.Show(exce.Message);   
      }   
      finally   
      {   
      sw.Close();   
      br.Close();   
      fs.Close();   
      }   
        
      }   
      

  5.   

    假设你的form名字叫Form1
    用toolbox里面的RichTextBox工具在form1上面拖一个RichTextBox对象叫做richTextBox1
    注册Form1的Load事件,        private void Form1_Load(object sender, EventArgs e)
            {
                richTextBox1.Text = "This is red, this is blue";
                richTextBox1.Select(0, "This is red".Length);
                richTextBox1.SelectionColor = Color.Red;
                richTextBox1.Select("This is red,".Length, "  this is blue".Length);
                richTextBox1.SelectionColor = Color.Blue;
            }运行程序,可以看到一个有彩色字的textbox
      

  6.   


    咦?回错帖子了
    楼主sorry,请忽略
      

  7.   

    用6喽的读写bin文件的方法就可以读出来不是乱码了,如果想要节省图片空间,上面的方法你还不满意,那就压缩图片吧,可能效果还原出来就不如以前了,
      

  8.   

    按照jpeg格式编码然后转换成base64?
    现在的压缩算法都会利用到全部ascii字符吧...