private void save(DibMd dibMd, string dstPath) { if (dibMd == null) { return; } string dstSuffix = Path.GetExtension(dstPath).ToLower(); FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_ICO; FREE_IMAGE_SAVE_FLAGS flag = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE; switch (dstSuffix) { case ".png": format = FREE_IMAGE_FORMAT.FIF_PNG; flag = FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION; break; case ".jpg": format = FREE_IMAGE_FORMAT.FIF_JPEG; flag = FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD; break; case ".bmp": format = FREE_IMAGE_FORMAT.FIF_BMP; flag = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE; break; case ".ico": default: break; } FreeImage.Save(format, dibMd.dib, dstPath, flag); //bool isOk = FreeImage.Save(FREE_IMAGE_FORMAT.FIF_PNG, dibOut, dstPath + ".png", FREE_IMAGE_SAVE_FLAGS.PNG_INTERLACED); //FreeImage.Unload(dibMd.dib); //FreeImage.Unload(dib); }
private void saveToStream(DibMd dibMd, string dstSuffix, Stream stream) { if (dibMd == null) { return; } //string dstSuffix = Path.GetExtension(dstPath).ToLower(); FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_ICO; FREE_IMAGE_SAVE_FLAGS flag = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE; switch (dstSuffix) { case ".png": format = FREE_IMAGE_FORMAT.FIF_PNG; flag = FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION; break; case ".jpg": format = FREE_IMAGE_FORMAT.FIF_JPEG; flag = FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD; break; case ".bmp": format = FREE_IMAGE_FORMAT.FIF_BMP; flag = FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE; break; case ".ico": default: break; } FreeImage.SaveToStream(dibMd.dib, stream, format, flag); }
private DibMd formatOutput(DibMd dibMd, int icoSize, int bpp) { if (dibMd == null) { return(null); } uint width = FreeImage.GetWidth(dibMd.dib); uint height = FreeImage.GetHeight(dibMd.dib); try { FIBITMAP dibOut = formatImage(dibMd.dib, icoSize, bpp); return(new DibMd(dibOut)); } catch (Exception) { } return(null); }
//private void ergDir(string path, ref List<string> lstPath) { // if(!Directory.Exists(path)) { // return; // } // DirectoryInfo info = new DirectoryInfo(path); // foreach(FileInfo NextFile in info.GetFiles()) { // if(NextFile.Name == "0-0-11.grid") // continue; // // 获取文件完整路径 // string heatmappath = NextFile.FullName; // } //} public string convert(string[] srcMultiPath, string dstDir, string multiSizeBpp, string outType = "auto", string operate = "rename", bool mergeOutput = false) { string rstInfo = "Success"; //HashSet<int> hsIconSize = new HashSet<int>(); //for(int i = 0; i < lstSupportIconSize.Length; ++i) { // hsIconSize.Add(lstSupportIconSize[i]); //} //HashSet<int> hsBpp = new HashSet<int>(); //for(int i = 0; i < lstSupportBpp.Length; ++i) { // hsBpp.Add(lstSupportBpp[i]); //} List <int> lstIcoSize = new List <int>(); List <int> lstIcoBpp = new List <int>(); // check param : outType if (!hsSupportOutType.Contains(outType)) { return(getErrorInfo(outType)); } // check param : operate if (!hsSupportOperate.Contains(operate)) { return(getErrorInfo(operate)); } // format param : size & bpp; string[] arr = multiSizeBpp.Split(new string[] { ";", ";" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arr.Length; ++i) { string[] arr2 = arr[i].Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries); if (arr2.Length <= 0) { continue; } int size; int bpp = 32; bool isOk = int.TryParse(arr2[0], out size); if (!isOk || !hsIconSize.Contains(size)) { return(getErrorInfo(multiSizeBpp)); } if (arr2.Length >= 2) { isOk = int.TryParse(arr2[1], out bpp); if (!isOk) { return(getErrorInfo(multiSizeBpp)); } } if (!hsBpp.Contains(bpp)) { return(getErrorInfo(multiSizeBpp)); } lstIcoSize.Add(size); lstIcoBpp.Add(bpp); } if (lstIcoSize.Count == 0) { lstIcoSize.Add(48); lstIcoBpp.Add(32); } //List<string> lstSrcPath = new List<string>(); //for(int i = 0; i < srcMultiPath.Length; ++i) { // if(Directory.Exists(path)) { // continue; // } //} for (int j = 0; j < srcMultiPath.Length; ++j) { List <DibMd> lstData = new List <DibMd>(); string path = srcMultiPath[j]; if (!File.Exists(path) && !Directory.Exists(path)) { continue; } string suffix = Path.GetExtension(path).ToLower(); string dir = Path.GetDirectoryName(path) + "/"; if (dstDir != "") { dir = dstDir + "/"; Directory.CreateDirectory(dir); } string outSuffix = getOutFileSuffix(suffix, outType); // 只有输出格式ico的才能合并输出 bool realMergeOutput = mergeOutput; if (outSuffix != ".ico") { realMergeOutput = false; } string mergeDstPath = dir + Path.GetFileNameWithoutExtension(path) + outSuffix; if (realMergeOutput) { if (File.Exists(mergeDstPath)) { switch (operate) { case "jump": continue; case "overwrite": break; case "cancel": return("Cancel"); case "rename": default: mergeDstPath = renameDstFileName(mergeDstPath); break; } } } for (int i = 0; i < lstIcoSize.Count; ++i) { //if(Directory.Exists(path)) { // continue; //} string fname = Path.GetFileNameWithoutExtension(path); if (!realMergeOutput && !isDefaultIcon(lstIcoSize[i], lstIcoBpp[i])) { fname = $"{fname}_{lstIcoSize[i]}_{lstIcoBpp[i]}"; } string dstPath = dir + fname + outSuffix; if (File.Exists(dstPath)) { switch (operate) { case "jump": continue; case "overwrite": break; case "cancel": return("Cancel"); case "rename": default: dstPath = renameDstFileName(dstPath); break; } } //convert(path, dstPath, lstIcoSize[i], lstIcoBpp[i]); try { DibMd md = loadFile(path, lstIcoSize[i]); DibMd outMd = formatOutput(md, lstIcoSize[i], lstIcoBpp[i]); md?.Dispose(); if (outMd == null) { continue; } if (realMergeOutput) { lstData.Add(outMd); } else { save(outMd, dstPath); } } catch (Exception) { } } // 合并输出 if (realMergeOutput && lstData.Count > 0) { try { FIMULTIBITMAP fmb = FreeImage.OpenMultiBitmap(FREE_IMAGE_FORMAT.FIF_ICO, mergeDstPath, true, false, false, FREE_IMAGE_LOAD_FLAGS.ICO_MAKEALPHA); for (var i = 0; i < lstData.Count; ++i) { FreeImage.AppendPage(fmb, lstData[i].dib); lstData[i].Dispose(); } lstData.Clear(); FreeImage.CloseMultiBitmapEx(ref fmb, FREE_IMAGE_SAVE_FLAGS.BMP_SAVE_RLE); } catch (Exception) { } } } return(rstInfo); }