private void test() { return; string strTitle = ";System.title =\"模板工程\";"; string strW = ";scWidth =1024;"; string strH = ";scHeight =768;"; Regex regTitle = new Regex(@"\s*;\s*System.title\s*="); Regex regW = new Regex(@"\s*;\s*scWidth\s*="); Regex regH = new Regex(@"\s*;\s*scHeight\s*="); bool ret = false; ret = regTitle.IsMatch(strTitle); ret = regW.IsMatch(strW); ret = regH.IsMatch(strH); string[] layouts = Directory.GetFiles(_curConfig.ThemeDataFolder, WizardConfig.UI_LAYOUT); // 测试tjs值读取 foreach (string layout in layouts) { using (StreamReader r = new StreamReader(layout)) { TjsParser parser = new TjsParser(); TjsValue val = null; do { val = parser.Parse(r); } while (val != null); } } // 测试tjs符号读取 using (StreamReader r = new StreamReader(layouts[0])) { TjsParser parser = new TjsParser(); TjsParser.Token token = null; do { token = parser.GetNext(r); } while (token != null && token.t != TjsParser.TokenType.Unknow); } // 资源转换器对象的测试用例 ResConfig config = new ResConfig(); config.files.Add(new ResFile(@"a.png")); config.files.Add(new ResFile(@"b.png")); config.name = "TestTest"; config.path = @"c:\"; config.Save(@"c:\test.xml"); ResConfig newConfig = ResConfig.Load(@"c:\test.xml"); ResConverter cov = new ResConverter(); cov.Start(config, @"d:\", 1024, 768, 1920, 1080); }
// 拷贝并缩放文件 void ConvertFiles(string srcPath, int sw, int sh, string destPath, int dw, int dh) { // 源文件列表 List <string> srcFiles = new List <string>(); try { bool ignoreCopy = (srcPath.ToLower() == destPath.ToLower()); // 建立目录并获取文件列表 CreateDir(srcPath, destPath, srcFiles); // 建立图片转换配置,用于记录需要转换的图片文件,其他文件则直接拷贝 ResConfig resource = new ResConfig(); resource.path = srcPath; resource.name = WizardConfig.NAME_DEFAULT_THEME; // 遍历所有文件 int cutLen = srcPath.Length; foreach (string srcfile in srcFiles) { // 截掉模板目录以径获取相对路径 string relFile = srcfile.Substring(cutLen + 1); // 取得扩展名 string ext = Path.GetExtension(relFile).ToLower(); if ( // 宽高如果和源文件相同那就不用转换了 (sw != dw || sh != dh) && // 忽略某些图片 !WizardConfig.IgnorePicture(relFile) && // 只转换这些扩展名对应的文件 (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")) { // 是图片则添加到转换器中 resource.files.Add(new ResFile(relFile)); } else if (!ignoreCopy) { // 直接拷贝 OnLogging(string.Format("拷贝{0}", relFile)); File.Copy(srcfile, Path.Combine(destPath, relFile), true); } } OnLogging("图片转换中……"); if (resource.files.Count > 0) { // 调用资源转换器方法,并开始转换 Start(resource, destPath, sw, sh, dw, dh); } } catch (System.Exception e) { Console.WriteLine(e.Message); } }
// 从文件中生成ResConfig对象 static public ResConfig Load(string filename) { try { using (StreamReader r = new StreamReader(string.Format(filename))) { XmlSerializer serializer = new XmlSerializer(typeof(ResConfig)); ResConfig obj = (ResConfig)serializer.Deserialize(r); r.Close(); obj.path = Path.GetDirectoryName(filename); return(obj); } } catch (System.Exception) { return(null); } }
// 按照配置转换指定的资源 public void Start( // 资源文件列表 ResConfig config, // 目标路径 string destFolder, // 源屏幕大小 int srcWidth, int srcHeight, // 目标屏幕大小 int destWidth, int destHeight) { // 忽略无效参数 if (srcWidth <= 0 || srcHeight <= 0 || destWidth <= 0 || destHeight <= 0) { return; } if (srcWidth == destWidth && srcHeight == destHeight) { return; } int cur = 0; // 以root为根目录载入所有图片并缩放 string baseDir = config.path; foreach (ResFile file in config.files) { OnNotifyProcess(file.path, ++cur, config.files.Count); string inputFile = Path.GetFullPath(Path.Combine(baseDir, file.path)); string destFile = Path.GetFullPath(Path.Combine(destFolder, file.path)); // 选择质量参数 Quality q = Quality.HIGH; if (file.quality.ToLower() == ResFile.QUALITY_LOW) { q = Quality.LOW; } else if (file.quality.ToLower() == ResFile.QUALITY_NORMAL) { q = Quality.NORMAL; } try { Bitmap dest = null; ImageFormat format = null; // 读取源图片 using (Bitmap source = new Bitmap(inputFile)) { // 根据策略计算区域映射(尚未实现) Dictionary <Rectangle, Rectangle> rects = CalcRects(source, srcWidth, srcHeight, destWidth, destHeight); // 实施转换 dest = Scale(source, srcWidth, srcHeight, destWidth, destHeight, q, rects); format = source.RawFormat; } if (dest != null) { dest.Save(destFile, format); } // 转换完毕 } catch (System.Exception e) { // 转换出现错误 Console.WriteLine(e.Message); } } }