把黑白图片压缩成dat文件.
大神们,我要做bad apple字符动画,压缩图片的时候总是压缩不好,源代码如下.using System;
using System.Drawing;
using System.IO;
using System.IO.Compression;
//这是压缩的.
namespace BadAppleSharpEncoder
{
class Program
{
static void Main(string[] args)
{
//获取in目录下所有文件 未作异常处理
string[] path = Directory.GetFileSystemEntries("images - 副本");
//保存为Gzip压缩的文本流
FileStream fileStream = new FileStream("badapple.dat", FileMode.Create, FileAccess.Write);
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
StreamWriter sw = new StreamWriter(compressionStream);
//遍历每个像素点
// string img = "images";
for (int x=0;x<50;x++)
{
//Console.WriteLine(path[x]);
Bitmap bitmap = new Bitmap(path[x]);
for (int i = 0; i < bitmap.Height; i++)
{
for (int j = 0; j <bitmap.Width ; j++)
{
Color color = bitmap.GetPixel(j, i);
if (color.R > 250)
{
//Console.Write("1");
sw.Write(" ");
}
else
{
//Console.Write("0");
sw.Write("0");
}
}
//Console.WriteLine();
sw.WriteLine();
Console.Clear();
Console.WriteLine("{0} of {1} completed! ",x,path.Length);
}
bitmap.Dispose();
}
sw.Dispose();
}
}
}
//这是读取
using System;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Timers;
using NAudio.Wave;
namespace BadAppleSharpPlayer
{
class Program
{
private static StreamReader reader;
static void Main(string[] args)
{
//打开压缩文本流
FileStream fileStream = new FileStream("badapple.dat", FileMode.Open, FileAccess.Read);
GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress);
reader = new StreamReader(compressionStream);
//初始化控制台参数
Console.Title = "BadAppleSharp 0.1";
Console.WindowWidth = 80;
//调用播放器播放背景音乐
WaveStream mp3Reader = new Mp3FileReader(@"BadApple.mp3");
WaveStream pcmStreamer = WaveFormatConversionStream.CreatePcmStream(mp3Reader);
WaveStream blockAlignedStream = new BlockAlignReductionStream(pcmStreamer);
WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
waveOut.Init(blockAlignedStream);
waveOut.Play();
//初始化定时器
Timer timer = new Timer(55);
timer.Elapsed+=new ElapsedEventHandler(Render);
timer.Enabled = true;
Console.ReadKey();
waveOut.Dispose();
blockAlignedStream.Dispose();
pcmStreamer.Dispose();
mp3Reader.Dispose();
}
//渲染一帧
public static void Render(Object sender,ElapsedEventArgs e)
{
{
Console.Clear();
StringBuilder data=new StringBuilder(2500);
for (int i = 0; i < 30; i++)
{
data.Append(reader.ReadLine());
}
Console.Write(data.ToString());
}
}
}
}
读取倒是好使,就是压缩的时候老是出现乱码,我估计是我压缩判断颜色的时候出错拉,求解啊.
补充:.NET技术 , C#