This commit is contained in:
Manuel Kamper 2019-03-22 23:07:44 +01:00
parent c84e6e2700
commit 6c2ccf5aff
7 changed files with 267 additions and 1 deletions

25
Mk0.Tools.Images.sln Normal file
View File

@ -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

View File

@ -0,0 +1,113 @@
using System.Drawing;
namespace Mk0.Tools.Images
{
public class BorderCropper
{
#region CropUnwantedBackground
/// <summary>
/// Entfernt gleichfarbige Bildränder
/// </summary>
/// <param name="bmp">Bild dessen Ränder entfernt werden sollen</param>
/// <returns>Bild dem die Ränder entfernt wurden</returns>
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
}
}

View File

@ -0,0 +1,21 @@
using System.Drawing;
namespace Mk0.Tools.Images
{
public class CopyImage
{
/// <summary>
/// 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
/// </summary>
/// <param name="path">Pfad des Bildes</param>
/// <returns>Kopie des Bildes</returns>
public static Image GetCopyImage(string path)
{
using (Image im = Image.FromFile(path))
{
Bitmap bm = new Bitmap(im);
return bm;
}
}
}
}

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8C546D79-1695-41B5-9AF3-3A30E9EF0337}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Mk0.Tools.Images</RootNamespace>
<AssemblyName>Mk0.Tools.Images</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="BorderCropper.cs" />
<Compile Include="GetCopyImage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resolution.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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")]

View File

@ -0,0 +1,18 @@
using System;
using System.Drawing;
namespace Mk0.Tools.Images
{
public class Resolution
{
/// <summary>
/// Gibt die Auflösung eines Bildes zurück
/// </summary>
/// <param name="img">Bild</param>
/// <returns>Auflösung</returns>
public static string GetImageResolution(Image img)
{
return Math.Round((((double)img.Height * (double)img.Width) / 1000000), 2) + " MP";
}
}
}

View File

@ -1 +1,8 @@
# Mk0.Tools.Images
# 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