Created
September 30, 2018 17:47
-
-
Save abfo/dfac3298cb5d8d756dd0a5210361b05d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void CrushAllImages() | |
{ | |
try | |
{ | |
string appDataRoot = HostingEnvironment.MapPath("~/App_Data"); | |
if (appDataRoot == null) | |
{ | |
return; | |
} | |
DirectoryInfo directoryInfo = new DirectoryInfo(appDataRoot); | |
FileInfo[] pngs = directoryInfo.GetFiles("*.png", SearchOption.AllDirectories); | |
foreach (FileInfo png in pngs) | |
{ | |
CrushImage(png.FullName); | |
} | |
} | |
catch (Exception ex) | |
{ | |
//... | |
} | |
} | |
public static void CrushImage(string fullPath) | |
{ | |
if (string.IsNullOrEmpty(fullPath)) | |
{ | |
return; | |
} | |
try | |
{ | |
string markerPath = Path.ChangeExtension(fullPath, ".cng"); | |
if (File.Exists(markerPath)) | |
{ | |
return; | |
} | |
string crushExe = HostingEnvironment.MapPath("~/App_Data/pngcrush_1_7_77_w32.exe"); | |
ProcessStartInfo psi = new ProcessStartInfo(crushExe, string.Format(CultureInfo.InvariantCulture, "\"{0}\" \"{1}\"", fullPath, markerPath)); | |
psi.UseShellExecute = false; | |
psi.CreateNoWindow = true; | |
psi.LoadUserProfile = false; | |
psi.WorkingDirectory = HostingEnvironment.MapPath("~/App_Data"); | |
Process p = Process.Start(psi); | |
if (p == null) | |
{ | |
throw new InvalidOperationException("No Process!"); | |
} | |
p.WaitForExit(); | |
if (File.Exists(markerPath)) | |
{ | |
if (p.ExitCode == 0) | |
{ | |
File.Copy(markerPath, fullPath, true); | |
File.WriteAllText(markerPath, "Processed"); | |
} | |
else | |
{ | |
SiteLog.Log.Add(LogSeverity.Error, "CrushImage Failed (non-0 exit code) for " + fullPath); | |
File.Delete(markerPath); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment