windows窗口程序,获取当前文件夹下的“preview”结尾的png,转换成1位的bmp
2017-09-19 16:05
519 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Threading; using System.IO; namespace TextureConversion2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //存储获取到的图片路径 List filePaths = new List(); void load() { string imgtype = "*.PNG";//"*.PNG|*.JPG|*.GIF|*BMP" string[] ImageType = imgtype.Split('|'); for (int i = 0; i < ImageType.Length; i++) { //获取指定文件夹下所有的图片路径 string[] dirs = Directory.GetFiles(Environment.CurrentDirectory.ToString(), ImageType[i]); for (int j = 0; j < dirs.Length; j++) {//判断是否为preview图片 int _index = dirs[j].IndexOf("."); string _str = dirs[j].Substring(_index - 7, 7); if (_str == "preview") { filePaths.Add(dirs[j]); } } } } //压缩测试 void CompressTest() { for (int i = 0; i < filePaths.Count; i++) { TurnBMP(filePaths[i]); } } /// /// 根据图片路径返回图片的字节流byte[] /// /// 图片路径 /// 返回的字节流 private static byte[] getImageByte(string imagePath) { FileStream files = new FileStream(imagePath, FileMode.Open); byte[] imgByte = new byte[files.Length]; files.Read(imgByte, 0, imgByte.Length); files.Close(); files.Dispose(); return imgByte; } Bitmap bitmap = null; Bitmap bitmap2 = null; //图片另存成1位的BMP格式(单色图,只有黑白两个颜色)内有两种方法,都可以 void TurnBMP(string texturePath) { //FileStream fs = new FileStream(texturePath, FileMode.Open, FileAccess.Read, FileShare.Read); //byte[] buffer = new byte[fs.Length]; //int length = 0; //int ibyteRead = 0; //do //{ // length = fs.Read(buffer, ibyteRead, buffer.Length - ibyteRead); // ibyteRead += length; //} //while (length > 0); //MemoryStream mfs = new MemoryStream(buffer); //fs.Close(); //fs.Dispose(); //Image bmp1 = Image.FromStream(mfs); //string _path; //int _index = texturePath.IndexOf("."); //string _str = texturePath.Substring(0, _index + 1); //_path = _str + ".bmp"; //bmp1.Save(_path, ImageFormat.Bmp); //////ljz添加 ////bitmap2 = bitmap.Clone(new Rectangle(0, 0, bmp1.Width, bitmap.Height), PixelFormat.Format1bppIndexed);//重点在于Clone,获得原bitmap副本就可以设置PixcelFormat了,要注意矩形大小不能超过原Bitmap ////bitmap2.Save(_path, ImageFormat.Bmp); ////bitmap2.Dispose(); //bmp1.Dispose(); //mfs.Close(); //mfs.Dispose(); ////////////////////////////////////////////////////////////////////////////////////////// //Console.Write("ok"); //bitmap = new Bitmap(texturePath); //BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); //bitmap2 = new Bitmap(bitmap.Width, bitmap.Height, data.Stride, PixelFormat.Format1bppIndexed, data.Scan0); //string _path; //int _index = texturePath.IndexOf("."); //string _str = texturePath.Substring(0, _index + 1); //_path = _str + ".bmp"; //bitmap2.Save(_path, ImageFormat.Bmp); //bitmap.UnlockBits(data); ////////////////////////////////////////////////////////////////////////////////////////////// string _path; int _index = texturePath.IndexOf("."); string _str = texturePath.Substring(0, _index); _path = _str + ".bmp"; if (File.Exists(_path)) File.Delete(_path); bitmap = new Bitmap(texturePath); bitmap2 = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format1bppIndexed);//重点在于Clone,获得原bitmap副本就可以设置PixcelFormat了,要注意矩形大小不能超过原Bitmap bitmap2.Save(_path, ImageFormat.Bmp); if (bitmap != null) { bitmap.Dispose(); } if (bitmap2 != null) { bitmap2.Dispose(); } //////////////////////////////////////////////////////////////////////////////////////////////// //////创建一个bitmap类型的bmp变量来读取文件。 ////Bitmap bmp = new Bitmap(texturePath); //using (Bitmap bmp = new Bitmap(texturePath), bmp2 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed)) //{ // ////新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。 // //Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format1bppIndexed); // //将第一个bmp拷贝到bmp2中 // Graphics draw = Graphics.FromImage(bmp); // draw.DrawImage(bmp2, 0, 0); // string _path; // int _index = texturePath.IndexOf("."); // string _str = texturePath.Substring(0, _index + 1); // _path = _str + ".bmp"; // bmp2.Save(_path, ImageFormat.Bmp); // draw.Dispose(); // //bmp.Dispose();//释放bmp文件资源 //} //////////////////////////////////////////////////////////////////////////////////////////////////// //创建一个bitmap类型的bmp变量来读取文件。 //Bitmap bmp = new Bitmap(texturePath); ////新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。 //Bitmap bmp2 = new Bitmap(1024, 768, PixelFormat.Format1bppIndexed); ////将第一个bmp拷贝到bmp2中 //Graphics draw = Graphics.FromImage(bmp2); //draw.DrawImage(bmp, 0, 0); //string _path; //int _index = texturePath.IndexOf("."); //string _str = texturePath.Substring(0, _index + 1); //_path = _str + ".bmp"; ////bmp2.Save(_path, ImageFormat.Bmp); //draw.Dispose(); //bmp.Dispose();//释放bmp文件资源 // //bmp2.Dispose(); //////////////////////////////////////////////////////////////////////////// //创建一个bitmap类型的bmp变量来读取文件。 //Bitmap bmp = new Bitmap(texturePath); //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。 //Bitmap bmp2 = new Bitmap(1024, 768, PixelFormat.Format16bppRgb555); //将第一个bmp拷贝到bmp2中 //Graphics draw = Graphics.FromImage(bmp2); ////draw.DrawImage(bmp, 0, 0); //bmp2.Save(_path, ImageFormat.Bmp); //draw.Dispose(); //bmp.Dispose();//释放bmp文件资源 //bmp2.Dispose(); ///////////////////////////////////////////////////////////////////////////////////////////// //Image img = Image.FromFile(texturePath); //Bitmap bm = new Bitmap(img.Width, img.Height,PixelFormat.Format1bppIndexed); //Graphics g = Graphics.FromImage(bm); //g.Clear(Color.Transparent); //g.DrawImage(img, 0, 0); //img.Dispose(); ////File.Delete(path); //string _path; //int _index = texturePath.IndexOf("."); //string _str = texturePath.Substring(0, _index + 1); //_path = _str + ".bmp"; //bm.Save(_path, System.Drawing.Imaging.ImageFormat.Bmp);//把jpg转成bmp //g.Dispose(); //bm.Dispose(); ////////////////////////////////////////////////////////////////////////////////////////// //try //{ // FileStream files = new FileStream(texturePath, FileMode.Open); // Bitmap bmp2 = new Bitmap(files); // string _path; // int _index = texturePath.IndexOf("."); // string _str = texturePath.Substring(0, _index + 1); // _path = _str + ".bmp"; // bmp2.Save(_path, ImageFormat.Bmp); // bmp2.Dispose(); // files.Close(); // files.Dispose(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.ToString()); // Console.WriteLine(ex.ToString()); //} } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { load(); CompressTest(); } } }
相关文章推荐
- 【截图】WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照
- webdriver获取当前最新打开的windows窗口_10_5
- 获取当前执行程序的路径,并用资源管理器打开日志文件夹
- Windows程序获取鼠标下窗口句柄的例子
- windows获取其他程序窗口的内容
- Windows获取文件夹内所有png文件路径
- windows和linux下获取当前程序路径以及cpu数
- windows和linux下获取当前程序路径以及cpu数
- WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照
- 如何将iPhone程序中的png图片转换为可读的png图片(Windows版本)
- Windows & Linux(Unix) 获取当前程序运行目录
- Windows & Linux(Unix) 获取当前程序运行目录
- windows下c语言获取程序当前的执行目录,读文件的代码片
- Windows下获取当前程序文件名或完整路径方法
- windows下c语言获取程序当前的执行目录,读文件的代码片
- 利用C/C++编写程序以获取文件夹内所有子文件名,windows和Linux两个版本都有
- windows下c语言获取程序当前的执行目录,读文件的代码片
- C#获取当前活动窗口进程、进程ID、程序路径
- VC/MFC程序中获取当前Windows操作系统版本名称(2016-04-25修改)
- 在java程序中如何能获取到另一个程序的windows句柄,又如何使用这个句柄向这个窗口发送消息