Merge branch 'develope' of https://github.com/clayschaad/LegoArtTool into develope
# Conflicts: # LegoArtTool/LegoArtImageGenerationService.cs
This commit is contained in:
commit
cdc438c030
5 changed files with 73 additions and 14 deletions
|
|
@ -71,7 +71,7 @@ namespace LegoArt
|
||||||
var sourceBitmap = bitmapHelperService.Scale(reducedBitMap, 1);
|
var sourceBitmap = bitmapHelperService.Scale(reducedBitMap, 1);
|
||||||
sourceImage.Source = imageHelperService.LoadToImage(sourceBitmap);
|
sourceImage.Source = imageHelperService.LoadToImage(sourceBitmap);
|
||||||
|
|
||||||
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, 20);
|
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, legoArtColorService.GetLegoColors());
|
||||||
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
||||||
|
|
||||||
btnInstructionsPersister.Visibility = Visibility.Visible;
|
btnInstructionsPersister.Visibility = Visibility.Visible;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using LegoArtTool.Pixels;
|
using LegoArtTool.Pixels;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
namespace LegoArtTool
|
namespace LegoArtTool
|
||||||
|
|
@ -29,9 +30,9 @@ namespace LegoArtTool
|
||||||
return scaledBitmap;
|
return scaledBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, int pixelSize)
|
public Bitmap ConvertToPixelMatrix(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors)
|
||||||
{
|
{
|
||||||
var pixelMatrix = new PixelMatrix(pixelSize, bitmap);
|
var pixelMatrix = new PixelMatrix(bitmap, legoArtColors);
|
||||||
return pixelMatrix.ConvertBitmap();
|
return pixelMatrix.ConvertBitmap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
42
LegoArtTool/Pixels/GraphicExtensions.cs
Normal file
42
LegoArtTool/Pixels/GraphicExtensions.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Drawing2D;
|
||||||
|
|
||||||
|
namespace LegoArtTool.Pixels
|
||||||
|
{
|
||||||
|
public static class GraphicsExtensions
|
||||||
|
{
|
||||||
|
public static void DrawCircle(this Graphics g, Pen pen, float centerX, float centerY, float radius)
|
||||||
|
{
|
||||||
|
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
|
||||||
|
{
|
||||||
|
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DrawText(this Graphics g, Brush brush, Font font, int size, StringAlignment alignment, string text)
|
||||||
|
{
|
||||||
|
var format = new StringFormat();
|
||||||
|
format.LineAlignment = alignment;
|
||||||
|
format.Alignment = alignment;
|
||||||
|
|
||||||
|
var rectf = new RectangleF(0, 0, size, size);
|
||||||
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||||
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||||
|
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||||
|
g.DrawString(text, font, brush, rectf, format);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Font GetIdealFontSize(this Graphics g, int size, string referenceText, Font preferedFont)
|
||||||
|
{
|
||||||
|
var room = new Size(size, size);
|
||||||
|
var realSize = g.MeasureString(referenceText, preferedFont);
|
||||||
|
var heightScaleRatio = room.Height / realSize.Height;
|
||||||
|
var widthScaleRatio = room.Width / realSize.Width;
|
||||||
|
var scaleRatio = (heightScaleRatio < widthScaleRatio) ? heightScaleRatio : widthScaleRatio;
|
||||||
|
var scaleFontSize = preferedFont.Size * scaleRatio;
|
||||||
|
return new Font(preferedFont.FontFamily, scaleFontSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,27 +6,42 @@ namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
public Color Color { get; private set; }
|
public Color Color { get; private set; }
|
||||||
public int Number { get; private set; }
|
public int Number { get; private set; }
|
||||||
public int Size { get; private set; }
|
|
||||||
|
|
||||||
public Pixel(Color color, int size, int number)
|
public const int PixelSize = 50;
|
||||||
|
private const int NumberSize = 20;
|
||||||
|
|
||||||
|
public Pixel(Color color, int number)
|
||||||
{
|
{
|
||||||
Color = color;
|
Color = color;
|
||||||
Size = size;
|
|
||||||
Number = number;
|
Number = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap GetBitmap()
|
public Bitmap GetBitmap()
|
||||||
{
|
{
|
||||||
var bitmap = new Bitmap(Size, Size);
|
var bitmap = new Bitmap(PixelSize, PixelSize);
|
||||||
for (int x = 0; x < Size; x++)
|
for (int x = 0; x < PixelSize; x++)
|
||||||
{
|
{
|
||||||
for (int y = 0; y < Size; y++)
|
for (int y = 0; y < PixelSize; y++)
|
||||||
{
|
{
|
||||||
bitmap.SetPixel(x, y, Color);
|
bitmap.SetPixel(x, y, Color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DrawNumber(bitmap, Number);
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DrawNumber(Bitmap bitmap, int number)
|
||||||
|
{
|
||||||
|
|
||||||
|
using (var g = Graphics.FromImage(bitmap))
|
||||||
|
{
|
||||||
|
g.FillCircle(Brushes.White, PixelSize / 2, PixelSize / 2, NumberSize / 2);
|
||||||
|
var font = g.GetIdealFontSize(NumberSize, "15", new Font("Arial", 8));
|
||||||
|
g.DrawText(Brushes.Black, font, PixelSize, StringAlignment.Center, number.ToString());
|
||||||
|
g.Flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace LegoArtTool.Pixels
|
namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
|
|
@ -7,14 +9,12 @@ namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
public Pixel[,] Pixels { get; private set; }
|
public Pixel[,] Pixels { get; private set; }
|
||||||
public int MatrixSize { get; private set; }
|
public int MatrixSize { get; private set; }
|
||||||
public int PixelSize { get; private set; }
|
|
||||||
|
|
||||||
private Random rnd = new Random();
|
private Random rnd = new Random();
|
||||||
|
|
||||||
public PixelMatrix(int pixelSize, Bitmap sourceBitmap)
|
public PixelMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
|
||||||
{
|
{
|
||||||
MatrixSize = sourceBitmap.Width;
|
MatrixSize = sourceBitmap.Width;
|
||||||
PixelSize = pixelSize;
|
|
||||||
Pixels = new Pixel[MatrixSize, MatrixSize];
|
Pixels = new Pixel[MatrixSize, MatrixSize];
|
||||||
|
|
||||||
for (int x = 0; x < MatrixSize; x++)
|
for (int x = 0; x < MatrixSize; x++)
|
||||||
|
|
@ -23,14 +23,15 @@ namespace LegoArtTool.Pixels
|
||||||
{
|
{
|
||||||
var pixel = sourceBitmap.GetPixel(x, y);
|
var pixel = sourceBitmap.GetPixel(x, y);
|
||||||
var sourceColor = Color.FromArgb(pixel.ToArgb());
|
var sourceColor = Color.FromArgb(pixel.ToArgb());
|
||||||
Pixels[x, y] = new Pixel(sourceColor, PixelSize, 9);
|
var legoNumber = legoArtColors.Single(l => l.Color == sourceColor).LegoNumber;
|
||||||
|
Pixels[x, y] = new Pixel(sourceColor, legoNumber);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap ConvertBitmap()
|
public Bitmap ConvertBitmap()
|
||||||
{
|
{
|
||||||
var bitmapSize = MatrixSize * PixelSize;
|
var bitmapSize = MatrixSize * Pixel.PixelSize;
|
||||||
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
||||||
using (var g = Graphics.FromImage(bitmap))
|
using (var g = Graphics.FromImage(bitmap))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue