C# :: Aufgabe #180 :: Lösung #2
3 Lösungen

#180
Farben/Pixel sortieren
Fortgeschrittener - C#
von KawaiiShox
- 29.06.2017 um 11:39 Uhr
Es soll ein Programm erstellt werden, welches zufällig erstellte Pixel nach der Farbe sortieren kann.
Das Programm soll dabei ungefähr so aussehen, wie auf Bild1.
Im ersten Teil der Aufgabe soll es , durch das Drücken eines Buttons, möglich sein ein Panel mit Pixeln, die alle eine zufällig generierte Farbe besitzen, zu füllen. (Bild2)
Als nächstes sollen dann alle Pixel des Panels sortiert werden(Bild3). In diesem Beispiel habe ich die Pixel nach ihrem "HUE" sortiert. Spannend wäre aber auch zu wissen, wie es aussieht, wenn man die Pixel nach einem anderem Kriterium sortiert.
Wer möchte, kann aber auch die Pixel eines bestimmten Bildes sortieren.
Das Programm soll dabei ungefähr so aussehen, wie auf Bild1.
Im ersten Teil der Aufgabe soll es , durch das Drücken eines Buttons, möglich sein ein Panel mit Pixeln, die alle eine zufällig generierte Farbe besitzen, zu füllen. (Bild2)
Als nächstes sollen dann alle Pixel des Panels sortiert werden(Bild3). In diesem Beispiel habe ich die Pixel nach ihrem "HUE" sortiert. Spannend wäre aber auch zu wissen, wie es aussieht, wenn man die Pixel nach einem anderem Kriterium sortiert.
Wer möchte, kann aber auch die Pixel eines bestimmten Bildes sortieren.
#2

von hollst (13980 Punkte)
- 21.07.2017 um 14:14 Uhr
Hier noch ein Lösungsvorschlag in WPF, wo es ja bekanntlich (leider) keine PictureBox mehr gibt,
deshalb muss man sich mit WPF schon ganz schön winden.
C#-Code
C#-Code
deshalb muss man sich mit WPF schon ganz schön winden.

using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Drawing; using System.IO; namespace aufgabe_180_WPF { public partial class MainWindow : Window { Random rand = new Random(); public int[] random_pixel; int Empty_BitMapImage_h = 400, Empty_BitMapImage_w = 400; public static BitmapImage Create_Empty_BitMapImage(int width, int height) { Bitmap bitmap = new Bitmap(width, height); BitmapImage image = new BitmapImage(); using (MemoryStream memory = new MemoryStream()) { bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); memory.Position = 0; image.BeginInit(); image.StreamSource = memory; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); } return image; } public MainWindow() { InitializeComponent(); } public void bt_1_Click(object sender, RoutedEventArgs e) { BitmapImage EmptyImage = Create_Empty_BitMapImage(Empty_BitMapImage_w, Empty_BitMapImage_h); BitmapSource bitmapSource = new FormatConvertedBitmap(EmptyImage, PixelFormats.Default, null, 0); WriteableBitmap RandomImage = new WriteableBitmap(bitmapSource); int h = RandomImage.PixelHeight; int w = RandomImage.PixelWidth; int[] pixelData = new int[w * h]; int widthInByte = 4 * w; this.random_pixel = new int[w * h]; for (var i = 0; i < random_pixel.Length; i++) random_pixel[i] = rand.Next(int.MinValue, int.MaxValue); RandomImage.WritePixels(new Int32Rect(0, 0, w, h), random_pixel, widthInByte, 0); image_org.Source = RandomImage; } private void bt_2_Click(object sender, RoutedEventArgs e) { BitmapImage EmptyImage = Create_Empty_BitMapImage(Empty_BitMapImage_w, Empty_BitMapImage_h); BitmapSource bitmapSource = new FormatConvertedBitmap(EmptyImage, PixelFormats.Default, null, 0); WriteableBitmap modifiedImage = new WriteableBitmap(bitmapSource); int h = modifiedImage.PixelHeight; int w = modifiedImage.PixelWidth; int[] new_pixelData = new int[w * h]; int widthInByte = 4 * w; int counter1 = 0, counter2 = 0; for (var y = 0; y < w; y++) { Dictionary<int, double> dict = new Dictionary<int, double>(); for (int x = 0; x < h; x++) { System.Drawing.Color c = System.Drawing.Color.FromArgb(random_pixel[counter1]); dict.Add(counter1, c.GetHue()); counter1++; } var items = from pair in dict orderby pair.Value ascending select pair; foreach (KeyValuePair<int, double> index_x in items) { new_pixelData[counter2] = random_pixel[index_x.Key]; counter2++; } } modifiedImage.WritePixels(new Int32Rect(0, 0, w, h), new_pixelData, widthInByte, 0); image_sorted.Source = modifiedImage; BmpBitmapEncoder bmpEncoder = new BmpBitmapEncoder(); bmpEncoder.Frames.Add(BitmapFrame.Create(modifiedImage)); using (System.IO.FileStream file = System.IO.File.OpenWrite("test.bmp")) { bmpEncoder.Save(file); } } } }

<Window x:Class="aufgabe_180_WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:aufgabe_180_WPF" mc:Ignorable="d" Title="colour sorting WPF" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="45"/> <RowDefinition/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Button x:Name="bt_1" Content=" random colours " HorizontalAlignment="Center" Margin="5" VerticalAlignment="Center" Click="bt_1_Click" Grid.ColumnSpan="2"/> <Button x:Name="bt_2" Content=" sort colours " Grid.Column="2" HorizontalAlignment="Center" Margin="5" VerticalAlignment="Center" Click="bt_2_Click" Grid.ColumnSpan="2"/> <Image x:Name="image_org" Grid.ColumnSpan="2" Margin="10,0,10,10" Grid.Row="1" Grid.RowSpan="2" /> <Image x:Name="image_sorted" Grid.ColumnSpan="2" Margin="10,0,10,10" Grid.Row="1" Grid.Column="2" Grid.RowSpan="2"/> </Grid> </Window>
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1