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();
}
}
}
windows窗口程序,获取当前文件夹下的“preview”结尾的png,转换成1位的bmp
最新推荐文章于 2024-07-06 03:02:40 发布