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("XX");
            
            //保存为Gzip压缩的文本流
            FileStream fileStream = new FileStream("badapple.dat", FileMode.Create, FileAccess.Write);
            GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Compress);
            StreamWriter sw = new StreamWriter(compressionStream);            //遍历每个像素点
            for (int x=0;x<path.Length;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 > 200)
                        {
                            //Console.Write("1");
                            sw.Write(" ");
                        }
                        else
                        {
                            //Console.Write("0");
                            sw.Write("#");
                        }
                    }
                    //Console.WriteLine();
                    sw.WriteLine();
                    Console.Clear();
                    Console.WriteLine("{0} of {1} completed! ",x,path.Length);
                }
                bitmap.Dispose();
            }
            sw.Dispose();
        }
       
    }
}
------------------------------
我想把XX文件夹下所有txt或者图片文件压缩成bat格式的文件,这样写总报错,怎么写?

解决方案 »

  1.   

    http://blog.csdn.net/fengyarongaa/article/details/6346986
      

  2.   


     /// <summary>
            /// 打包成Rar
            /// </summary>
            /// <param name="patch"></param>
            /// <param name="rarPatch"></param>
            /// <param name="rarName"></param>
            public void CompressRAR(string patch, string rarPatch, string rarName)
            {
                string the_rar;
                RegistryKey the_Reg;
                object the_Obj;
                string the_Info;
                ProcessStartInfo the_StartInfo;
                Process the_Process;
                try
                {
                    the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/WinRAR.exe");
                    the_Obj = the_Reg.GetValue("");
                    the_rar = the_Obj.ToString();
                    the_Reg.Close();
                    the_rar = the_rar.Substring(1, the_rar.Length - 7);
                    Directory.CreateDirectory(patch);
                    //命令参数
                    //the_Info = " a    " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
                    the_Info = " a    " + rarName + " " + patch + " -r"; ;
                    the_StartInfo = new ProcessStartInfo();
                    the_StartInfo.FileName = the_rar;
                    the_StartInfo.Arguments = the_Info;
                    the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    //打包文件存放目录
                    the_StartInfo.WorkingDirectory = rarPatch;
                    the_Process = new Process();
                    the_Process.StartInfo = the_StartInfo;
                    the_Process.Start();
                    the_Process.WaitForExit();
                    the_Process.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
      

  3.   

    先找个高效的压缩软件,一定要绿色的,例如rar.exe,就一个exe足够。然后复制到项目中,用Process外部调用这个压缩软件进行压缩,给定的参数看该软件说明,只要命令行下输入程序名+ /?即可看到参数说明。
    .NET虽然自带压缩类,但是算法很低级,效率差,毕竟高效的算法是要收费的,微软不会免费提供。