指正源文件并把它转成二进制然后转换成图片进行保存至指定位置
时间:2010-08-31 来源:裴鹏
//指定源文件夹
string[] dirs = System.IO.Directory.GetDirectories("E:\\AutoSite\\img\\");
foreach (string dir in dirs)
{
string fileName = dir.Substring(dir.LastIndexOf("\\") + 1); //获取文件夹名
string[] files = System.IO.Directory.GetFiles(dir + "\\"); //子文件夹
foreach (string file in files)
{
//转换成二进制
FileInfo f = new FileInfo(file);
int FileLen = int.Parse(f.Length.ToString());
Byte[] FileData = new Byte[FileLen];
FileStream hp = new FileStream(file, FileMode.Open);
System.IO.Stream sr = System.IO.Stream.Synchronized(hp);
sr.Read(FileData, 0, FileLen);
//把二进制转换成图片进行保存至指定位置
MemoryStream ms = new MemoryStream((Byte[])FileData);//imgname是图片的名字
Bitmap img = new Bitmap(ms);
//创建文件夹
string strAutoID = "file1"; //DDListAutoName.SelectedItem.Value;
string filepath = "Upimg\\"; //目标地址;
Random rand = new Random();
if (Directory.Exists(filepath + strAutoID))
{
string strImg = filepath + strAutoID + "\\" + ((rand.Next(100, 999)) + ".jpg");
img.Save(strImg);
}
else
{
Directory.CreateDirectory(filepath + strAutoID);
string strImg = filepath + strAutoID + "\\" + ((rand.Next(100, 999)) + ".jpg");
img.Save(strImg);
}
}
}