////////////////////////////////////////////////////////////////////////// public bool Crop(string fileName, out int modX, out int modY) { try { Bitmap image = new Bitmap(fileName, false); int origWidth = image.Width; int origHeight = image.Height; int cutLeft, cutRight, cutTop, cutBottom; GetBorders(image, out cutLeft, out cutRight, out cutTop, out cutBottom); if (cutLeft == image.Width) { // empty image cutLeft = cutTop = 0; cutRight = image.Width - 1; cutBottom = image.Height - 1; } else { cutLeft = Math.Max(0, cutLeft - 1); cutTop = Math.Max(0, cutTop - 1); cutRight = Math.Max(0, cutRight - 1); cutBottom = Math.Max(0, cutBottom - 1); } if (cutLeft == 0 && cutRight == 0 && cutTop == 0 && cutBottom == 0) { modX = modY = 0; return(false); } Rectangle rect = new Rectangle(cutLeft, cutTop, image.Width - cutRight - cutLeft, image.Height - cutBottom - cutTop); Bitmap newImage = image.Clone(rect, image.PixelFormat); image.Dispose(); newImage.Save(fileName); modX = -cutLeft; modY = -cutTop; int savedPixels = origWidth * origHeight - newImage.Width * newImage.Height; Optimizer.AddLog(string.Format("Cropped image '{0}' (saved {1} pixels).", fileName, savedPixels.ToString())); Optimizer.AddSavedPixels(savedPixels); newImage.Dispose(); return(true); } catch (Exception e) { Optimizer.AddLog("Error cropping image: " + e.Message); modX = modY = 0; return(false); } }
////////////////////////////////////////////////////////////////////////// public bool SaveToFile() { string content = ToString(); try { using (StreamWriter sw = new StreamWriter(FileName, false, Encoding.Default)) { sw.WriteLine(content); } Optimizer.AddLog(string.Format("Modified hotspots in sprite '{0}'.", FileName)); return(true); } catch (Exception e) { Optimizer.AddLog("Error saving sprite: " + e.Message); return(false); } }