diff --git a/ConvertTo16x16.csproj b/ConvertTo16x16.csproj index 8447e6e..21f5b58 100644 --- a/ConvertTo16x16.csproj +++ b/ConvertTo16x16.csproj @@ -45,6 +45,7 @@ + diff --git a/Program.cs b/Program.cs index db5e3e2..dd4f4aa 100644 --- a/Program.cs +++ b/Program.cs @@ -35,63 +35,80 @@ namespace ConvertTo16x16 //C:\Users\zamk\Bilder\RickNice.jpg - string bildName = "RickNice.jpg"; + 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($@"D:\Pictures16\{bildName}"); // + Bitmap bmp = new Bitmap($@"{Pfad}\{bildName}"); Bitmap outcome = new Bitmap(PixelAnzahl, PixelAnzahl); int Width = bmp.Width / PixelAnzahl; int Height = bmp.Height / PixelAnzahl; + int counter = 0; - - for (int i = 0; i < PixelAnzahl; i++) + Console.Write("Performing some task... "); + using (var progress = new ProgressBar()) { - for (int j = 0; j < PixelAnzahl; j++) + for (int i = 0; i < PixelAnzahl; i++) { - - List Argb = new List(); - - for (int x = Width * i; x < Width * (i + 1); x++) + for (int j = 0; j < PixelAnzahl; j++) { - for (int y = Height * j; y < Height * (j + 1); y++) + + List Argb = new List(); + + for (int x = Width * i; x < Width * (i + 1); x++) { - - bool Exists = false; - - foreach (var item in Argb) + for (int y = Height * j; y < Height * (j + 1); y++) { - if (bmp.GetPixel(x, y).ToArgb() == item.Argb) //Zu switch case umbauen + + bool Exists = false; + + foreach (var item in Argb) { - Exists = true; - item.Anzahl += 1; + if (bmp.GetPixel(x, y).ToArgb() == item.Argb) //Zu switch case umbauen + { + Exists = true; + item.Anzahl += 1; + } } - } - if (!Exists) - { - Argb.Add(new Count(bmp.GetPixel(x, y).ToArgb(), new Point(x, y), 1)); + if (!Exists) + { + 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); + + progress.Report(report); } } - } + Count BiggestItem = new Count(0); - Count BiggestItem = new Count(0); - - foreach (var item in Argb) - { - if (item.Anzahl > BiggestItem.Anzahl) + foreach (var item in Argb) { - BiggestItem = item; + if (item.Anzahl > BiggestItem.Anzahl) + { + BiggestItem = item; + } } + + outcome.SetPixel(i, j, Color.FromArgb(BiggestItem.Argb)); + } - outcome.SetPixel(i, j, Color.FromArgb(BiggestItem.Argb)); + } } - - outcome.Save($@"D:\Pictures16\16x16{bildName}"); + outcome.Save($@"{Pfad}\16x16{bildName}"); } } diff --git a/ProgressBar.cs b/ProgressBar.cs new file mode 100644 index 0000000..fc30f2f --- /dev/null +++ b/ProgressBar.cs @@ -0,0 +1,102 @@ +using System; +using System.Text; +using System.Threading; + +/// +/// An ASCII progress bar +/// +public class ProgressBar : IDisposable, IProgress +{ + private const int blockCount = 10; + private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8); + private const string animation = @"|/-\"; + + private readonly Timer timer; + + private double currentProgress = 0; + private string currentText = string.Empty; + private bool disposed = false; + private int animationIndex = 0; + + public ProgressBar() + { + timer = new Timer(TimerHandler); + + // A progress bar is only for temporary display in a console window. + // If the console output is redirected to a file, draw nothing. + // Otherwise, we'll end up with a lot of garbage in the target file. + if (!Console.IsOutputRedirected) + { + ResetTimer(); + } + } + + public void Report(double value) + { + // Make sure value is in [0..1] range + value = Math.Max(0, Math.Min(1, value)); + Interlocked.Exchange(ref currentProgress, value); + } + + private void TimerHandler(object state) + { + lock (timer) + { + if (disposed) return; + + int progressBlockCount = (int)(currentProgress * blockCount); + int percent = (int)(currentProgress * 100); + string text = string.Format("[{0}{1}] {2,3}% {3}", + new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount), + percent, + animation[animationIndex++ % animation.Length]); + UpdateText(text); + + ResetTimer(); + } + } + + private void UpdateText(string text) + { + // Get length of common portion + int commonPrefixLength = 0; + int commonLength = Math.Min(currentText.Length, text.Length); + while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength]) + { + commonPrefixLength++; + } + + // Backtrack to the first differing character + StringBuilder outputBuilder = new StringBuilder(); + outputBuilder.Append('\b', currentText.Length - commonPrefixLength); + + // Output new suffix + outputBuilder.Append(text.Substring(commonPrefixLength)); + + // If the new text is shorter than the old one: delete overlapping characters + int overlapCount = currentText.Length - text.Length; + if (overlapCount > 0) + { + outputBuilder.Append(' ', overlapCount); + outputBuilder.Append('\b', overlapCount); + } + + Console.Write(outputBuilder); + currentText = text; + } + + private void ResetTimer() + { + timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1)); + } + + public void Dispose() + { + lock (timer) + { + disposed = true; + UpdateText(string.Empty); + } + } + +} \ No newline at end of file diff --git a/bin/Debug/ConvertTo16x16.exe b/bin/Debug/ConvertTo16x16.exe index 062e255..4e93c9c 100644 Binary files a/bin/Debug/ConvertTo16x16.exe and b/bin/Debug/ConvertTo16x16.exe differ diff --git a/bin/Debug/ConvertTo16x16.pdb b/bin/Debug/ConvertTo16x16.pdb index 090a115..2cfd92f 100644 Binary files a/bin/Debug/ConvertTo16x16.pdb and b/bin/Debug/ConvertTo16x16.pdb differ diff --git a/obj/Debug/ConvertTo16x16.exe b/obj/Debug/ConvertTo16x16.exe index 062e255..4e93c9c 100644 Binary files a/obj/Debug/ConvertTo16x16.exe and b/obj/Debug/ConvertTo16x16.exe differ diff --git a/obj/Debug/ConvertTo16x16.pdb b/obj/Debug/ConvertTo16x16.pdb index 090a115..2cfd92f 100644 Binary files a/obj/Debug/ConvertTo16x16.pdb and b/obj/Debug/ConvertTo16x16.pdb differ