public void Save() { //ConvertTo8bpp(ResultImage, FileInfo.FullName); if (IsOptimzed) { ResultImage.Save(FileInfo.FullName, ImageFormat.Png); } }
private void button3_Click(object sender, EventArgs e) { if (ResultImage != null) { DialogResult result = saveFileDialog1.ShowDialog(); // Show the dialog. (File explorer) if (result == DialogResult.OK) // Test result. { ResultImage.Save(saveFileDialog1.FileName); } } else { MessageBox.Show("Please first convert image.", "Warning"); } }
public void Optimze() { if (!IsOptimzed) { return; } if (PublishTarget.Current.IsCacheEnabled && FileCacheCenter.IsInCache(FileInfo, Md5, FileCacheOperation.ImageOptimzed)) { var fileInfo = FileCacheCenter.GetCacheFileInfo(FileInfo, Md5, FileCacheOperation.ImageOptimzed); var cacheItem = FileCacheCenter.GetCacheFileItem(FileInfo, Md5, FileCacheOperation.ImageOptimzed); if (ResultImage != null) { ResultImage.Dispose(); GC.Collect(); ResultImage = null; } //ResultImage = (Bitmap)Bitmap.FromFile(fileInfo.FullName); ResultSize = GetImageSize(fileInfo); if (cacheItem.TextureRect != null) { Rectangle rect = new Rectangle(); rect.X = (int)cacheItem.TextureRect.Origin.X; rect.Y = (int)cacheItem.TextureRect.Origin.Y; rect.Width = (int)cacheItem.TextureRect.Size.Width; rect.Height = (int)cacheItem.TextureRect.Size.Height; TextureRect = rect; } if (cacheItem.Offset != null) { Point pos = new Point(); pos.X = (int)cacheItem.Offset.X; pos.Y = (int)cacheItem.Offset.Y; Offset = pos; } Logger.LogInfoLine("FileCache Hit:Optimze {0} ", FileInfo.FullName); UpdateFileInfo(fileInfo); } else { var originalImage = (Bitmap)Bitmap.FromFile(FileInfo.FullName); var leftBottom = new Point(int.MaxValue, int.MaxValue); var rightTop = Point.Empty; bool isBreak = false; for (int i = 0; i < originalImage.Width; i++) { for (int j = 0; j < originalImage.Height; j++) { var color = originalImage.GetPixel(i, j); if (color.A != 0) { leftBottom.X = i; isBreak = true; break; } } if (isBreak) { break; } } isBreak = false; for (int i = originalImage.Width - 1; i >= 0; i--) { for (int j = 0; j < originalImage.Height; j++) { var color = originalImage.GetPixel(i, j); if (color.A != 0) { rightTop.X = i; isBreak = true; break; } } if (isBreak) { break; } } isBreak = false; for (int i = 0; i < originalImage.Height; i++) { for (int j = 0; j < originalImage.Width; j++) { var color = originalImage.GetPixel(j, i); if (color.A != 0) { leftBottom.Y = i; isBreak = true; break; } } if (isBreak) { break; } } isBreak = false; for (int i = originalImage.Height - 1; i >= 0; i--) { for (int j = 0; j < originalImage.Width; j++) { var color = originalImage.GetPixel(j, i); if (color.A != 0) { rightTop.Y = i; isBreak = true; break; } } if (isBreak) { break; } } var clipRect = new Rectangle(leftBottom, new Size(rightTop.X - leftBottom.X + 1, rightTop.Y - leftBottom.Y + 1)); if (leftBottom.X == int.MaxValue || leftBottom.Y == int.MaxValue) { ResultImage = originalImage; Logger.LogError("Empty image:{0}\r\n", FileInfo); } else { if (clipRect.Size != originalImage.Size) { Offset = new Point(leftBottom.X, originalImage.Size.Height - clipRect.Bottom); ResultImage = ClipImage(originalImage, clipRect); originalImage.Dispose(); ResultSize = ResultImage.Size; } else { ResultImage = originalImage; } } if (PublishTarget.Current.IsCacheEnabled) { var newFileInfo = FileCacheCenter.GetCacheNewFileInfo(FileInfo.FullName, FileCacheOperation.ImageOptimzed); if (newFileInfo.Exists) { newFileInfo.Delete(); } ResultImage.Save(newFileInfo.FullName, ImageFormat.Png); FileCacheCenter.AddToCacheWithoutCopy(Md5, FileInfo, TextureRect, Offset, FileCacheOperation.ImageOptimzed); UpdateFileInfo(newFileInfo); } else { UpdateFileInfo(FileInfo); } ResultImage.Dispose(); GC.Collect(); ResultImage = null; } //Logger.LogInfoLine("Optimze image:{0}", ToString()); }