Skip to content

Instantly share code, notes, and snippets.

@abfo
Created September 30, 2018 17:47
Show Gist options
  • Save abfo/dfac3298cb5d8d756dd0a5210361b05d to your computer and use it in GitHub Desktop.
Save abfo/dfac3298cb5d8d756dd0a5210361b05d to your computer and use it in GitHub Desktop.
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