Added pixel matrix
This commit is contained in:
parent
74b4ad83e3
commit
91f205ea84
5 changed files with 122 additions and 13 deletions
|
|
@ -13,7 +13,7 @@
|
||||||
<RowDefinition Height="*"/>
|
<RowDefinition Height="*"/>
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="*" />
|
<ColumnDefinition Width="400" />
|
||||||
<ColumnDefinition Width="*" />
|
<ColumnDefinition Width="*" />
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
|
<StackPanel Grid.Row="2" Orientation="Vertical" Name="parentStackPanel" VerticalAlignment="Top">
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Vertical">
|
<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="sourceImage" Stretch="None"/>
|
||||||
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="scaledImage" Stretch="None"/>
|
<Image HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Top" Name="scaledImage" Stretch="None"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using Microsoft.Win32;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Input;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using System.Windows.Shapes;
|
using System.Windows.Shapes;
|
||||||
|
|
||||||
|
|
@ -32,7 +33,18 @@ namespace LegoArt
|
||||||
if (openFileDialog.ShowDialog() == true)
|
if (openFileDialog.ShowDialog() == true)
|
||||||
{
|
{
|
||||||
tbImagePath.Text = openFileDialog.FileName;
|
tbImagePath.Text = openFileDialog.FileName;
|
||||||
|
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
Mouse.OverrideCursor = Cursors.Wait;
|
||||||
|
});
|
||||||
|
|
||||||
LoadImage(openFileDialog.FileName);
|
LoadImage(openFileDialog.FileName);
|
||||||
|
|
||||||
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
Mouse.OverrideCursor = null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,11 +53,12 @@ namespace LegoArt
|
||||||
var legoArtColors = legoArtColorService.ParseImage(path);
|
var legoArtColors = legoArtColorService.ParseImage(path);
|
||||||
if (legoArtColors != null)
|
if (legoArtColors != null)
|
||||||
{
|
{
|
||||||
var sourceBitamp = bitmapHelperService.ScaleImage(path, 1);
|
var bitmap = new System.Drawing.Bitmap(path);
|
||||||
|
var sourceBitamp = bitmapHelperService.Scale(bitmap, 1);
|
||||||
sourceImage.Source = imageHelperService.LoadToImage(sourceBitamp);
|
sourceImage.Source = imageHelperService.LoadToImage(sourceBitamp);
|
||||||
|
|
||||||
var scaledBitamp = bitmapHelperService.ScaleImage(path, 5);
|
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitamp, 20);
|
||||||
scaledImage.Source = imageHelperService.LoadToImage(scaledBitamp);
|
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
||||||
|
|
||||||
parentStackPanel.Children.Clear();
|
parentStackPanel.Children.Clear();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,26 @@
|
||||||
using System.Drawing;
|
using LegoArtTool.Pixels;
|
||||||
|
using System.Drawing;
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool
|
||||||
{
|
{
|
||||||
public class BitmapHelperService
|
public class BitmapHelperService
|
||||||
{
|
{
|
||||||
public Bitmap ScaleImage(string path, int scaleFactor)
|
public Bitmap Scale(Bitmap bitmap, int scaleFactor)
|
||||||
{
|
{
|
||||||
var inputBitmap = new Bitmap(path);
|
var scaledBitmap = new Bitmap(bitmap.Width * scaleFactor, bitmap.Height * scaleFactor);
|
||||||
var scaledBitmap = new Bitmap(inputBitmap.Width * scaleFactor, inputBitmap.Height * scaleFactor);
|
|
||||||
|
|
||||||
for (int x = 0; x < inputBitmap.Width; x++)
|
for (int x = 0; x < bitmap.Width; x++)
|
||||||
{
|
{
|
||||||
for (int y = 0; y < inputBitmap.Height; y++)
|
for (int y = 0; y < bitmap.Height; y++)
|
||||||
{
|
{
|
||||||
var sourcePixel = inputBitmap.GetPixel(x, y);
|
var sourcePixel = bitmap.GetPixel(x, y);
|
||||||
|
var sourceColor = Color.FromArgb(sourcePixel.ToArgb());
|
||||||
|
|
||||||
for (int scaleX = x * scaleFactor; scaleX < scaledBitmap.Width; scaleX++)
|
for (int scaleX = x * scaleFactor; scaleX < scaledBitmap.Width; scaleX++)
|
||||||
{
|
{
|
||||||
for (int scaleY = y * scaleFactor; scaleY < scaledBitmap.Height; scaleY++)
|
for (int scaleY = y * scaleFactor; scaleY < scaledBitmap.Height; scaleY++)
|
||||||
{
|
{
|
||||||
scaledBitmap.SetPixel(scaleX, scaleY, Color.FromArgb(sourcePixel.ToArgb()));
|
scaledBitmap.SetPixel(scaleX, scaleY, sourceColor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -26,5 +28,11 @@ namespace LegoArtTool
|
||||||
|
|
||||||
return scaledBitmap;
|
return scaledBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, int pixelSize)
|
||||||
|
{
|
||||||
|
var pixelMatrix = new PixelMatrix(pixelSize, bitmap);
|
||||||
|
return pixelMatrix.ConvertBitmap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
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