Draw number on pixels
This commit is contained in:
parent
21395633d2
commit
0bf7ef09b0
6 changed files with 74 additions and 15 deletions
|
|
@ -61,7 +61,7 @@ namespace LegoArt
|
|||
var sourceBitmap = bitmapHelperService.Scale(reducedBitMap, 1);
|
||||
sourceImage.Source = imageHelperService.LoadToImage(sourceBitmap);
|
||||
|
||||
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, 20);
|
||||
var convertedBitmap = bitmapHelperService.ConvertToPixelMatrix(sourceBitmap, legoArtColorService.GetLegoColors());
|
||||
scaledImage.Source = imageHelperService.LoadToImage(convertedBitmap);
|
||||
|
||||
parentStackPanel.Children.Clear();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using LegoArtTool.Pixels;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
namespace LegoArtTool
|
||||
|
|
@ -29,9 +30,9 @@ namespace LegoArtTool
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ namespace LegoArtTool
|
|||
int xPosition,
|
||||
int yPosition)
|
||||
{
|
||||
var legoArtColorOffsets = legoArtColorInfos.Select(i => ColorOffset(inputColor, i)).ToList();
|
||||
var legoArtColorOffsets = legoArtColorInfos.Select(i => ColourDistance(inputColor, i)).ToList();
|
||||
return new Pixel(xPosition, yPosition, legoArtColorOffsets);
|
||||
}
|
||||
|
||||
|
|
|
|||
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 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;
|
||||
Size = size;
|
||||
Number = number;
|
||||
}
|
||||
|
||||
public Bitmap GetBitmap()
|
||||
{
|
||||
var bitmap = new Bitmap(Size, Size);
|
||||
for (int x = 0; x < Size; x++)
|
||||
var bitmap = new Bitmap(PixelSize, PixelSize);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
DrawNumber(bitmap, Number);
|
||||
|
||||
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.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
|
||||
namespace LegoArtTool.Pixels
|
||||
{
|
||||
|
|
@ -7,14 +9,12 @@ namespace LegoArtTool.Pixels
|
|||
{
|
||||
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)
|
||||
public PixelMatrix(Bitmap sourceBitmap, List<LegoArtColorInfo> legoArtColors)
|
||||
{
|
||||
MatrixSize = sourceBitmap.Width;
|
||||
PixelSize = pixelSize;
|
||||
Pixels = new Pixel[MatrixSize, MatrixSize];
|
||||
|
||||
for (int x = 0; x < MatrixSize; x++)
|
||||
|
|
@ -23,14 +23,15 @@ namespace LegoArtTool.Pixels
|
|||
{
|
||||
var pixel = sourceBitmap.GetPixel(x, y);
|
||||
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()
|
||||
{
|
||||
var bitmapSize = MatrixSize * PixelSize;
|
||||
var bitmapSize = MatrixSize * Pixel.PixelSize;
|
||||
var bitmap = new Bitmap(bitmapSize, bitmapSize);
|
||||
using (var g = Graphics.FromImage(bitmap))
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue