把图片存储成文本的问题 把图片转换成二进制流存储时,几乎文本的大小与图片的大小一样大小.怎么保存文本更小一点?还有我用二进制存,打开的时候总可以看到乱码.怎么样才可以文本里就只二进制的数据? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 图片转换成Base64格式保存在xml中节省空间 首先编写实现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; } }} 这里提供的是字符串形式的,可以稍微改造提供对字节数组的Base64编码和解码的。实现很简单,你自己照葫芦画瓢吧。 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(); } } 假设你的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 咦?回错帖子了楼主sorry,请忽略 用6喽的读写bin文件的方法就可以读出来不是乱码了,如果想要节省图片空间,上面的方法你还不满意,那就压缩图片吧,可能效果还原出来就不如以前了, 按照jpeg格式编码然后转换成base64?现在的压缩算法都会利用到全部ascii字符吧... DataTable 如何实现自定义排序? 我的C#在XP系统中能正常运行,怎么在2003系统中没反应,也不报错 怎么把字符串输出到文件的结尾 大家帮我看看哪个地方错了,谢谢 存储过程怎么用? 有没有人开发过或者正在开发酒店管理软件的? 请高手指教局域网络连接 项目升级到2005后,为什么控件风格还是2003的 C#中web服务器与SQL服务器分开时,对SQL数据库进行备份 vs2005里的源代码管理? 大家知道firefox管理书签的那个tree效果是咋做的吗?用tree还是listbox? 关于listview的问题
节省空间
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;
}
}
}
{
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();
}
}
用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
咦?回错帖子了
楼主sorry,请忽略
现在的压缩算法都会利用到全部ascii字符吧...