From 44b651eb5c84c3b4118d2892b25b35a750e113fb Mon Sep 17 00:00:00 2001 From: niksedk Date: Fri, 20 Jan 2012 22:52:21 +0000 Subject: [PATCH] Improved left/right cropping for export images git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@940 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Forms/ExportPngXml.cs | 5 +++- src/Logic/NikseBitmap.cs | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Forms/ExportPngXml.cs b/src/Forms/ExportPngXml.cs index 6223bc9cd..362e3d84c 100644 --- a/src/Forms/ExportPngXml.cs +++ b/src/Forms/ExportPngXml.cs @@ -474,7 +474,10 @@ namespace Nikse.SubtitleEdit.Forms g.DrawPath(new Pen(parameter.BorderColor, parameter.BorderWidth), path); g.FillPath(new SolidBrush(parameter.SubtitleColor), path); g.Dispose(); - return bmp; + NikseBitmap nbmp = new NikseBitmap(bmp); + nbmp.CropTransparentSides(5); + return nbmp.GetBitmap(); + //return bmp; } private static string RemoveSubStationAlphaFormatting(string s) diff --git a/src/Logic/NikseBitmap.cs b/src/Logic/NikseBitmap.cs index a9c68ea7b..e16ae1767 100644 --- a/src/Logic/NikseBitmap.cs +++ b/src/Logic/NikseBitmap.cs @@ -357,6 +357,66 @@ namespace Nikse.SubtitleEdit.Logic return 0; } + public void CropTransparentSides(int maximumCropping) + { + int leftStart = 0; + bool done = false; + for (int x = 0; x < Width; x++) + { + for (int y = 0; y < Height; y++) + { + Color c = GetPixel(x, y); + if (c.A != 0) + { + done = true; + leftStart = x; + leftStart -= maximumCropping; + if (leftStart < 0) + leftStart = 0; + break; + } + if (done) + break; + } + } + + int rightEnd = Width-1; + done = false; + for (int x = Width - 1; x >= 0; x--) + { + for (int y = 0; y < Height; y++) + { + Color c = GetPixel(x, y); + if (c.A != 0) + { + done = true; + rightEnd = x; + rightEnd += maximumCropping; + if (rightEnd >= Width) + rightEnd = Width-1; + break; + } + if (done) + break; + } + } + + if (leftStart < 2 && rightEnd >= Width - 3) + return; + + int newWidth = rightEnd - leftStart + 1; + var newBitmapData = new byte[newWidth * Height * 4]; + int index = 0; + for (int y = 0; y < Height; y++) + { + int pixelAddress = (leftStart * 4) + (y * 4 * Width); + Buffer.BlockCopy(_bitmapData, pixelAddress, newBitmapData, index, 4 * newWidth); + index += 4 * newWidth; + } + Width = newWidth; + _bitmapData = newBitmapData; + } + public void Fill(Color color) { byte[] buffer = new byte[4];