Version 3, added Global settings that are saved. Added a Main menu

This commit is contained in:
Pablu23
2020-06-25 17:00:38 +02:00
parent 4e9d8fa51a
commit f39250fa5c
35 changed files with 105333 additions and 20 deletions

View File

@@ -4,6 +4,9 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace ConvertTo16x16
{
@@ -28,43 +31,188 @@ namespace ConvertTo16x16
}
class Settings
{
public string FileDir { get; set; }
public int PixelAnzahl { get; set; }
public bool RmBackground { get; set; }
public Color BackgroundCol { get; set; }
}
class Program
{
public static Settings settings = new Settings();
static void mainMenu()
{
while (true)
{
Console.WriteLine("[S]tart");
Console.WriteLine("S[e]ttings");
Console.WriteLine("E[n]d");
string input = Console.ReadLine();
switch (input.ToLower())
{
case "s":
if (settings == null || settings.FileDir == null || settings.PixelAnzahl < 16)
{
Console.WriteLine("You have to edit some Settings before continuing.");
Console.ReadKey(true);
break;
}
Console.Clear();
Start();
break;
case "e":
Console.Clear();
Settings();
break;
case "n":
Environment.Exit(0);
break;
default:
Console.WriteLine("Input not accepted");
Console.ReadKey(true);
Console.Clear();
break;
}
Console.Clear();
}
}
static void Main(string[] args)
{
LoadJson();
mainMenu();
}
//C:\Users\zamk\Bilder\RickNice.jpg
public static void Settings()
{
Console.Write("Geben sie den Pfad an : ");
if (settings.FileDir != null) {
Console.WriteLine($"\nPfad : {settings.FileDir}");
Console.SetCursorPosition(25, Console.CursorTop - 2);
}
settings.FileDir = Console.ReadLine();
Console.Write("\nGeben sie an Wieviele Pixel sie wollen (Empfohlen: 16 | 32) : ");
if (settings.PixelAnzahl > 15)
{
Console.WriteLine($"\nPixel Anzahl : {settings.PixelAnzahl}");
Console.SetCursorPosition(62, Console.CursorTop - 2);
}
settings.PixelAnzahl = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nGeben sie an Ob eine Hintergrundfarbe ignoriert werden soll : ");
Console.WriteLine("[T]rue");
Console.WriteLine("[F]alse");
if (settings.PixelAnzahl > 15)
{
Console.WriteLine($"Remove Background : {settings.RmBackground}");
Console.WriteLine($"Color : {settings.BackgroundCol}");
Console.SetCursorPosition(63, Console.CursorTop - 5);
}
string input = Console.ReadLine();
switch (input.ToLower())
{
case "t":
settings.RmBackground = true;
Console.WriteLine("Which Color should be removed");
Console.Write("Red : ");
int red = Convert.ToInt32(Console.ReadLine());
Console.Write("Green : ");
int green = Convert.ToInt32(Console.ReadLine());
Console.Write("Blue : ");
int blue = Convert.ToInt32(Console.ReadLine());
if (red > 255 || green > 255 || blue > 255)
{
Console.WriteLine("Input not accepted");
Console.ReadKey(true);
Console.Clear();
break;
}
settings.BackgroundCol = Color.FromArgb(red, green, blue);
break;
case "f":
settings.RmBackground = false;
break;
default:
Console.WriteLine("Input not accepted");
Console.ReadKey(true);
Console.Clear();
break;
}
SaveJson();
}
public static void LoadJson()
{
try
{
using (StreamReader r = new StreamReader(@"C:\Users\zamk\Documents\MyProjects\16x16\settings.json"))
{
settings = JsonConvert.DeserializeObject<Settings>(r.ReadToEnd());
/*string json = r.ReadToEnd();
List<Settings> items = JsonConvert.DeserializeObject<List<Settings>>(json);*/
}
}
catch (Exception e)
{
return;
}
using (StreamReader r = new StreamReader(@"C:\Users\zamk\Documents\MyProjects\16x16\settings.json"))
{
JsonConvert.DeserializeObject<Settings>(r.ReadToEnd());
/*string json = r.ReadToEnd();
List<Settings> items = JsonConvert.DeserializeObject<List<Settings>>(json);*/
}
}
public static void SaveJson()
{
string jsonString = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(@"C:\Users\zamk\Documents\MyProjects\16x16\settings.json", jsonString);
}
public static void Start()
{
Console.Write("Geben sie den Bildnamen an: ");
string bildName = Console.ReadLine();
Console.Write("Geben sie den Pfad an: ");
string Pfad = Console.ReadLine();
//string Pfad = @"D:\Pictures16";
//string bildName = "RickNice.jpg";
int PixelAnzahl = 16;
Bitmap bmp = new Bitmap($@"{Pfad}\{bildName}");
Bitmap outcome = new Bitmap(PixelAnzahl, PixelAnzahl);
int Width = bmp.Width / PixelAnzahl;
int Height = bmp.Height / PixelAnzahl;
Bitmap bmp = new Bitmap($@"{settings.FileDir}\{bildName}");
Bitmap outcome = new Bitmap(settings.PixelAnzahl, settings.PixelAnzahl);
int Width = bmp.Width / settings.PixelAnzahl;
int Height = bmp.Height / settings.PixelAnzahl;
int counter = 0;
Console.Write("Performing some task... ");
Console.Write("Berechnen... ");
using (var progress = new ProgressBar())
{
for (int i = 0; i < PixelAnzahl; i++)
for (int i = 0; i < settings.PixelAnzahl; i++)
{
for (int j = 0; j < PixelAnzahl; j++)
for (int j = 0; j < settings.PixelAnzahl; j++)
{
List<Count> Argb = new List<Count>();
for (int x = Width * i; x < Width * (i + 1); x++)
for (int x = Convert.ToInt32(Math.Ceiling((double)Width * i)); x < Math.Ceiling((double)Width * (i + 1)); x++)
{
for (int y = Height * j; y < Height * (j + 1); y++)
for (int y = Convert.ToInt32(Math.Ceiling((double)Height * j)); y < Math.Ceiling((double)Height * (j + 1)); y++)
{
bool Exists = false;
@@ -83,8 +231,8 @@ namespace ConvertTo16x16
Argb.Add(new Count(bmp.GetPixel(x, y).ToArgb(), new Point(x, y), 1));
}
counter++;
double report = Convert.ToDouble(counter) / Convert.ToDouble(16 * 16 * Width * Height);
double report = Convert.ToDouble(counter) / Convert.ToDouble(settings.PixelAnzahl * settings.PixelAnzahl * Width * Height);
progress.Report(report);
}
@@ -108,8 +256,9 @@ namespace ConvertTo16x16
}
}
outcome.Save($@"{Pfad}\16x16{bildName}");
outcome.Save($@"{settings.FileDir}\16x16{bildName}");
}
}
}