commit
5b8196d3fd
7 changed files with 228 additions and 34 deletions
39
LegoArt/ImageHelperService.cs
Normal file
39
LegoArt/ImageHelperService.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace LegoArt
|
||||
{
|
||||
public class ImageHelperService
|
||||
{
|
||||
public BitmapImage LoadToImage(System.Drawing.Bitmap bitmap)
|
||||
{
|
||||
using (var memory = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
|
||||
memory.Position = 0;
|
||||
var bitmapimage = new BitmapImage();
|
||||
bitmapimage.BeginInit();
|
||||
bitmapimage.StreamSource = memory;
|
||||
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmapimage.EndInit();
|
||||
return bitmapimage;
|
||||
}
|
||||
}
|
||||
|
||||
public BitmapImage LoadToImage(string path)
|
||||
{
|
||||
var bitmapimage = new BitmapImage();
|
||||
bitmapimage.BeginInit();
|
||||
bitmapimage.UriSource = new Uri(path);
|
||||
bitmapimage.EndInit();
|
||||
return bitmapimage;
|
||||
}
|
||||
|
||||
public Color ConvertDrawingToWindowsMediaColor(System.Drawing.Color inputColor)
|
||||
{
|
||||
return Color.FromArgb(inputColor.A, inputColor.R, inputColor.G, inputColor.B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,22 +5,31 @@
|
|||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:LegoArt"
|
||||
mc:Ignorable="d"
|
||||
Title="Lego Art Color Info" Height="500" Width="420">
|
||||
Title="Lego Art Color Info" Height="500" Width="800">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="25"/>
|
||||
<RowDefinition Height="20"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="400" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<StackPanel Grid.Row="0" Orientation="Horizontal" Height="25" VerticalAlignment="Top">
|
||||
<Label Width="50" Content="Bild:"/>
|
||||
<TextBox Width="300" x:Name="tbImagePath" />
|
||||
<Button Width="50" Name="btnImageChooser" Content="..." Click="btnImageChooser_Click"/>
|
||||
</StackPanel>
|
||||
|
||||
|
||||
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
|
||||
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
|
||||
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="sourceImage" Stretch="None"/>
|
||||
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="scaledImage" Stretch="None"/>
|
||||
</StackPanel>
|
||||
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using Microsoft.Win32;
|
|||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
|
|
@ -13,9 +14,17 @@ namespace LegoArt
|
|||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private readonly LegoArtColorService legoArtColorService;
|
||||
private readonly BitmapHelperService bitmapHelperService;
|
||||
private readonly ImageHelperService imageHelperService;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
legoArtColorService = new LegoArtColorService();
|
||||
bitmapHelperService = new BitmapHelperService();
|
||||
imageHelperService = new ImageHelperService();
|
||||
}
|
||||
|
||||
private void btnImageChooser_Click(object sender, RoutedEventArgs e)
|
||||
|
|
@ -24,16 +33,33 @@ namespace LegoArt
|
|||
if (openFileDialog.ShowDialog() == true)
|
||||
{
|
||||
tbImagePath.Text = openFileDialog.FileName;
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Mouse.OverrideCursor = Cursors.Wait;
|
||||
});
|
||||
|
||||
LoadImage(openFileDialog.FileName);
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
Mouse.OverrideCursor = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadImage(string path)
|
||||
{
|
||||
var legoArtColorService = new LegoArtColorService();
|
||||
var legoArtColors = legoArtColorService.ParseImage(path);
|
||||
var bitmap = new System.Drawing.Bitmap(path);
|
||||
var legoArtColors = legoArtColorService.ParseImage(bitmap);
|
||||
if (legoArtColors != null)
|
||||
{
|
||||
var sourceBitamp = bitmapHelperService.Scale(bitmap, 1);
|
||||
sourceImage.Source = imageHelperService.LoadToImage(sourceBitamp);
|
||||
|
||||
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitamp, 20);
|
||||
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
||||
|
||||
parentStackPanel.Children.Clear();
|
||||
|
||||
AddLine(System.Drawing.Color.White, "Have", "Needed", "RGB", "#", "Difference", System.Drawing.Color.White);
|
||||
|
|
@ -60,7 +86,7 @@ namespace LegoArt
|
|||
|
||||
private void AddLine(System.Drawing.Color color, string textHave, string textNeed, string textRgb, string textNumber, string textDifference, System.Drawing.Color statusColor)
|
||||
{
|
||||
var displayColor = ConvertDrawingToWindowsMediaColor(color);
|
||||
var displayColor = imageHelperService.ConvertDrawingToWindowsMediaColor(color);
|
||||
var rect = new Rectangle();
|
||||
rect.Width = 20;
|
||||
rect.Height = 20;
|
||||
|
|
@ -96,7 +122,7 @@ namespace LegoArt
|
|||
differenceTextBlock.TextAlignment = TextAlignment.Center;
|
||||
differenceTextBlock.Text = textDifference;
|
||||
|
||||
var displayStatusColor = ConvertDrawingToWindowsMediaColor(statusColor);
|
||||
var displayStatusColor = imageHelperService.ConvertDrawingToWindowsMediaColor(statusColor);
|
||||
var status = new Rectangle();
|
||||
status.Width = 20;
|
||||
status.Height = 20;
|
||||
|
|
@ -114,10 +140,5 @@ namespace LegoArt
|
|||
stackPanel.Children.Add(status);
|
||||
parentStackPanel.Children.Add(stackPanel);
|
||||
}
|
||||
|
||||
private Color ConvertDrawingToWindowsMediaColor(System.Drawing.Color inputColor)
|
||||
{
|
||||
return Color.FromArgb(inputColor.A, inputColor.R, inputColor.G, inputColor.B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
38
LegoArtTool/BitmapHelperService.cs
Normal file
38
LegoArtTool/BitmapHelperService.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using LegoArtTool.Pixels;
|
||||
using System.Drawing;
|
||||
|
||||
namespace LegoArtTool
|
||||
{
|
||||
public class BitmapHelperService
|
||||
{
|
||||
public Bitmap Scale(Bitmap bitmap, int scaleFactor)
|
||||
{
|
||||
var scaledBitmap = new Bitmap(bitmap.Width * scaleFactor, bitmap.Height * scaleFactor);
|
||||
|
||||
for (int x = 0; x < bitmap.Width; x++)
|
||||
{
|
||||
for (int y = 0; y < bitmap.Height; y++)
|
||||
{
|
||||
var sourcePixel = bitmap.GetPixel(x, y);
|
||||
var sourceColor = Color.FromArgb(sourcePixel.ToArgb());
|
||||
|
||||
for (int scaleX = x * scaleFactor; scaleX < scaledBitmap.Width; scaleX++)
|
||||
{
|
||||
for (int scaleY = y * scaleFactor; scaleY < scaledBitmap.Height; scaleY++)
|
||||
{
|
||||
scaledBitmap.SetPixel(scaleX, scaleY, sourceColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return scaledBitmap;
|
||||
}
|
||||
|
||||
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, int pixelSize)
|
||||
{
|
||||
var pixelMatrix = new PixelMatrix(pixelSize, bitmap);
|
||||
return pixelMatrix.ConvertBitmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,34 +6,33 @@ namespace LegoArtTool
|
|||
{
|
||||
public class LegoArtColorService
|
||||
{
|
||||
public static readonly List<LegoArtColorInfo> LegoColors;
|
||||
|
||||
static LegoArtColorService()
|
||||
public List<LegoArtColorInfo> GetLegoColors()
|
||||
{
|
||||
LegoColors = new List<LegoArtColorInfo>();
|
||||
var legoColors = new List<LegoArtColorInfo>();
|
||||
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 0, 0), 1, 698));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(102, 101, 96), 2, 141));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(167, 166, 162), 3, 51));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(246, 246, 248), 4, 149));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 53, 94), 5, 121));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(101, 135, 162), 6, 52));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(121, 196, 236), 7, 57));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(253, 104, 0), 8, 74));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(255, 170, 0), 9, 65));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(229, 205, 135), 10, 283));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(163, 129, 93), 11, 137));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(196, 115, 54), 12, 29));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(206, 81, 0), 13, 85));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(119, 40, 0), 14, 250));
|
||||
LegoColors.Add(new LegoArtColorInfo(Color.FromArgb(66, 23, 3), 15, 554));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 0, 0), 1, 698));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(102, 101, 96), 2, 141));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(167, 166, 162), 3, 51));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(246, 246, 248), 4, 149));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(0, 53, 94), 5, 121));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(101, 135, 162), 6, 52));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(121, 196, 236), 7, 57));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(253, 104, 0), 8, 74));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(255, 170, 0), 9, 65));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(229, 205, 135), 10, 283));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(163, 129, 93), 11, 137));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(196, 115, 54), 12, 29));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(206, 81, 0), 13, 85));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(119, 40, 0), 14, 250));
|
||||
legoColors.Add(new LegoArtColorInfo(Color.FromArgb(66, 23, 3), 15, 554));
|
||||
|
||||
return legoColors;
|
||||
}
|
||||
|
||||
public List<LegoArtColorInfo> ParseImage(string imagePath)
|
||||
public List<LegoArtColorInfo> ParseImage(Bitmap bitmap)
|
||||
{
|
||||
var parsedLegoColors = new List<LegoArtColorInfo>(LegoColors);
|
||||
var parsedLegoColors = GetLegoColors();
|
||||
|
||||
var bitmap = new Bitmap(imagePath);
|
||||
var width = bitmap.Width;
|
||||
var height = bitmap.Height;
|
||||
for (int h = 0; h < height; h++)
|
||||
|
|
|
|||
32
LegoArtTool/Pixels/Pixel.cs
Normal file
32
LegoArtTool/Pixels/Pixel.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System.Drawing;
|
||||
|
||||
namespace LegoArtTool.Pixels
|
||||
{
|
||||
public class Pixel
|
||||
{
|
||||
public Color Color { get; private set; }
|
||||
public int Number { get; private set; }
|
||||
public int Size { get; private set; }
|
||||
|
||||
public Pixel(Color color, int size, int number)
|
||||
{
|
||||
Color = color;
|
||||
Size = size;
|
||||
Number = number;
|
||||
}
|
||||
|
||||
public Bitmap GetBitmap()
|
||||
{
|
||||
var bitmap = new Bitmap(Size, Size);
|
||||
for (int x = 0; x < Size; x++)
|
||||
{
|
||||
for (int y = 0; y < Size; y++)
|
||||
{
|
||||
bitmap.SetPixel(x, y, Color);
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
LegoArtTool/Pixels/PixelMatrix.cs
Normal file
56
LegoArtTool/Pixels/PixelMatrix.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace LegoArtTool.Pixels
|
||||
{
|
||||
public class PixelMatrix
|
||||
{
|
||||
public Pixel[,] Pixels { get; private set; }
|
||||
public int MatrixSize { get; private set; }
|
||||
public int PixelSize { get; private set; }
|
||||
|
||||
private Random rnd = new Random();
|
||||
|
||||
public PixelMatrix(int pixelSize, Bitmap sourceBitmap)
|
||||
{
|
||||
MatrixSize = sourceBitmap.Width;
|
||||
PixelSize = pixelSize;
|
||||
Pixels = new Pixel[MatrixSize, MatrixSize];
|
||||
|
||||
for (int x = 0; x < MatrixSize; x++)
|
||||
{
|
||||
for (int y = 0; y < MatrixSize; y++)
|
||||
{
|
||||
var pixel = sourceBitmap.GetPixel(x, y);
|
||||
var sourceColor = Color.FromArgb(pixel.ToArgb());
|
||||
Pixels[x, y] = new Pixel(sourceColor, PixelSize, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Bitmap ConvertBitmap()
|
||||
{
|
||||
var bitmapSize = MatrixSize * PixelSize;
|
||||
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
||||
using (var g = Graphics.FromImage(bitmap))
|
||||
{
|
||||
for (int x = 0; x < MatrixSize; x++)
|
||||
{
|
||||
for (int y = 0; y < MatrixSize; y++)
|
||||
{
|
||||
var pixelBitmap = Pixels[x, y].GetBitmap();
|
||||
g.DrawImage(pixelBitmap, x * pixelBitmap.Width, y * pixelBitmap.Height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private Color GetRandomColor()
|
||||
{
|
||||
Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
|
||||
return randomColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue