From 6c2ccf5aff35cfcb70fd8b639992d33164fdbfb0 Mon Sep 17 00:00:00 2001 From: Manuel Kamper Date: Fri, 22 Mar 2019 23:07:44 +0100 Subject: [PATCH] Initial --- Mk0.Tools.Images.sln | 25 +++++ Mk0.Tools.Images/BorderCropper.cs | 113 ++++++++++++++++++++ Mk0.Tools.Images/GetCopyImage.cs | 21 ++++ Mk0.Tools.Images/Mk0.Tools.Images.csproj | 46 ++++++++ Mk0.Tools.Images/Properties/AssemblyInfo.cs | 36 +++++++ Mk0.Tools.Images/Resolution.cs | 18 ++++ README.md | 9 +- 7 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 Mk0.Tools.Images.sln create mode 100644 Mk0.Tools.Images/BorderCropper.cs create mode 100644 Mk0.Tools.Images/GetCopyImage.cs create mode 100644 Mk0.Tools.Images/Mk0.Tools.Images.csproj create mode 100644 Mk0.Tools.Images/Properties/AssemblyInfo.cs create mode 100644 Mk0.Tools.Images/Resolution.cs diff --git a/Mk0.Tools.Images.sln b/Mk0.Tools.Images.sln new file mode 100644 index 0000000..9d53d3b --- /dev/null +++ b/Mk0.Tools.Images.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.438 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mk0.Tools.Images", "Mk0.Tools.Images\Mk0.Tools.Images.csproj", "{8C546D79-1695-41B5-9AF3-3A30E9EF0337}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8C546D79-1695-41B5-9AF3-3A30E9EF0337}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C546D79-1695-41B5-9AF3-3A30E9EF0337}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C546D79-1695-41B5-9AF3-3A30E9EF0337}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C546D79-1695-41B5-9AF3-3A30E9EF0337}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {01A7CDBB-70D8-4270-B7D6-75AFE5ADAA30} + EndGlobalSection +EndGlobal diff --git a/Mk0.Tools.Images/BorderCropper.cs b/Mk0.Tools.Images/BorderCropper.cs new file mode 100644 index 0000000..59bc5fc --- /dev/null +++ b/Mk0.Tools.Images/BorderCropper.cs @@ -0,0 +1,113 @@ +using System.Drawing; + +namespace Mk0.Tools.Images +{ + public class BorderCropper + { + #region CropUnwantedBackground + /// + /// Entfernt gleichfarbige Bildränder + /// + /// Bild dessen Ränder entfernt werden sollen + /// Bild dem die Ränder entfernt wurden + public static Bitmap CropUnwantedBackground(Bitmap bmp) + { + var backColor = GetMatchedBackColor(bmp); + if (backColor.HasValue) + { + var bounds = GetImageBounds(bmp, backColor); + var diffX = bounds[1].X - bounds[0].X + 1; + var diffY = bounds[1].Y - bounds[0].Y + 1; + var croppedBmp = new Bitmap(diffX, diffY); + var g = Graphics.FromImage(croppedBmp); + var destRect = new Rectangle(0, 0, croppedBmp.Width, croppedBmp.Height); + var srcRect = new Rectangle(bounds[0].X, bounds[0].Y, diffX, diffY); + g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel); + return croppedBmp; + } + else + { + return null; + } + } + #endregion + + #region Private Methods + + #region GetImageBounds + private static Point[] GetImageBounds(Bitmap bmp, Color? backColor) + { + //-------------------------------------------------------------------- + // Finding the Bounds of Crop Area bu using Unsafe Code and Image Proccesing + Color c; + int width = bmp.Width, height = bmp.Height; + bool upperLeftPointFounded = false; + var bounds = new Point[2]; + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + c = bmp.GetPixel(x, y); + bool sameAsBackColor = ((c.R <= backColor.Value.R * 1.1 && c.R >= backColor.Value.R * 0.9) && + (c.G <= backColor.Value.G * 1.1 && c.G >= backColor.Value.G * 0.9) && + (c.B <= backColor.Value.B * 1.1 && c.B >= backColor.Value.B * 0.9)); + if (!sameAsBackColor) + { + if (!upperLeftPointFounded) + { + bounds[0] = new Point(x, y); + bounds[1] = new Point(x, y); + upperLeftPointFounded = true; + } + else + { + if (x > bounds[1].X) + bounds[1].X = x; + else if (x < bounds[0].X) + bounds[0].X = x; + if (y >= bounds[1].Y) + bounds[1].Y = y; + } + } + } + } + return bounds; + } + #endregion + + #region GetMatchedBackColor + private static Color? GetMatchedBackColor(Bitmap bmp) + { + // Getting The Background Color by checking Corners of Original Image + var corners = new Point[]{ + new Point(0, 0), + new Point(0, bmp.Height - 1), + new Point(bmp.Width - 1, 0), + new Point(bmp.Width - 1, bmp.Height - 1) + }; // four corners (Top, Left), (Top, Right), (Bottom, Left), (Bottom, Right) + for (int i = 0; i < 4; i++) + { + var cornerMatched = 0; + var backColor = bmp.GetPixel(corners[i].X, corners[i].Y); + for (int j = 0; j < 4; j++) + { + var cornerColor = bmp.GetPixel(corners[j].X, corners[j].Y);// Check RGB with some offset + if ((cornerColor.R <= backColor.R * 1.1 && cornerColor.R >= backColor.R * 0.9) && + (cornerColor.G <= backColor.G * 1.1 && cornerColor.G >= backColor.G * 0.9) && + (cornerColor.B <= backColor.B * 1.1 && cornerColor.B >= backColor.B * 0.9)) + { + cornerMatched++; + } + } + if (cornerMatched > 2) + { + return backColor; + } + } + return null; + } + #endregion + + #endregion + } +} diff --git a/Mk0.Tools.Images/GetCopyImage.cs b/Mk0.Tools.Images/GetCopyImage.cs new file mode 100644 index 0000000..e60bdad --- /dev/null +++ b/Mk0.Tools.Images/GetCopyImage.cs @@ -0,0 +1,21 @@ +using System.Drawing; + +namespace Mk0.Tools.Images +{ + public class CopyImage + { + /// + /// Helfer-Methode um eine Kopie eines Bildes zu erzeugen für Banner, weil es bei Anzeige im Banner nicht mehr am ursprünglichen Speicherort ist + /// + /// Pfad des Bildes + /// Kopie des Bildes + public static Image GetCopyImage(string path) + { + using (Image im = Image.FromFile(path)) + { + Bitmap bm = new Bitmap(im); + return bm; + } + } + } +} diff --git a/Mk0.Tools.Images/Mk0.Tools.Images.csproj b/Mk0.Tools.Images/Mk0.Tools.Images.csproj new file mode 100644 index 0000000..7b392fa --- /dev/null +++ b/Mk0.Tools.Images/Mk0.Tools.Images.csproj @@ -0,0 +1,46 @@ + + + + + Debug + AnyCPU + {8C546D79-1695-41B5-9AF3-3A30E9EF0337} + Library + Properties + Mk0.Tools.Images + Mk0.Tools.Images + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mk0.Tools.Images/Properties/AssemblyInfo.cs b/Mk0.Tools.Images/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..54892a6 --- /dev/null +++ b/Mk0.Tools.Images/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("Mk0.Tools.Images")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Mk0.Tools.Images")] +[assembly: AssemblyCopyright("Copyright © 2019 mk0.at")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("8c546d79-1695-41b5-9af3-3a30e9ef0337")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Mk0.Tools.Images/Resolution.cs b/Mk0.Tools.Images/Resolution.cs new file mode 100644 index 0000000..f5a6450 --- /dev/null +++ b/Mk0.Tools.Images/Resolution.cs @@ -0,0 +1,18 @@ +using System; +using System.Drawing; + +namespace Mk0.Tools.Images +{ + public class Resolution + { + /// + /// Gibt die Auflösung eines Bildes zurück + /// + /// Bild + /// Auflösung + public static string GetImageResolution(Image img) + { + return Math.Round((((double)img.Height * (double)img.Width) / 1000000), 2) + " MP"; + } + } +} diff --git a/README.md b/README.md index 118e6b0..d81efcb 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -# Mk0.Tools.Images \ No newline at end of file +# Mk0.Tools.Images +(C) 2019 mk0.at + +Allows the following operations + ++ BorderCropper - Crops off borders with the same colour from images ++ GetCopyImage - Returns a copy (in ram) of an image (from disk) ++ Resolution - Returns the resolution in megapixels of an image \ No newline at end of file