Draw instruction with dots

This commit is contained in:
Claudio Schaad 2020-12-03 23:00:13 +01:00
parent 27c1ca6a9b
commit d1ec026786
4 changed files with 59 additions and 17 deletions

View file

@ -77,7 +77,7 @@ namespace LegoArt
var d = new DataObject(DataFormats.Bitmap, legoArtImage.Source, true); var d = new DataObject(DataFormats.Bitmap, legoArtImage.Source, true);
var legoArtBitmap = d.GetData(typeof(Bitmap)) as Bitmap; var legoArtBitmap = d.GetData(typeof(Bitmap)) as Bitmap;
var buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoArtColorSet(currentLegoArtSet)); var buildingInstructionBitmap = buildingInstructionService.CreateBuildingInstructionImage(legoArtBitmap, legoArtColorService.GetLegoArtColorSet(currentLegoArtSet), true);
var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text); var outputPath = buildingInstructionService.PersistBuildingInstructions(buildingInstructionBitmap, tbImagePath.Text);
HideWaitCursor(); HideWaitCursor();

View file

@ -1,4 +1,5 @@
using System.Drawing; using System;
using System.Drawing;
namespace LegoArtTool.BuildingInstruction namespace LegoArtTool.BuildingInstruction
{ {
@ -8,7 +9,7 @@ namespace LegoArtTool.BuildingInstruction
public int Number { get; private set; } public int Number { get; private set; }
public const int PixelSize = 50; public const int PixelSize = 50;
private const int NumberSize = 20; private const int NumberSize = 25;
public BuildingInstructionElement(Color color, int number) public BuildingInstructionElement(Color color, int number)
{ {
@ -16,25 +17,57 @@ namespace LegoArtTool.BuildingInstruction
Number = number; Number = number;
} }
public Bitmap GetBitmap() public Bitmap GetBitmap(bool useDots)
{ {
var bitmap = new Bitmap(PixelSize, PixelSize); var bitmap = new Bitmap(PixelSize, PixelSize);
for (int x = 0; x < PixelSize; x++)
{
for (int y = 0; y < PixelSize; y++)
{
bitmap.SetPixel(x, y, Color);
}
}
DrawNumber(bitmap, Number); if (useDots)
{
DrawDot(bitmap, Color);
DrawNumber(bitmap, Number, Color);
}
else
{
DrawPixelBlock(bitmap, Color);
DrawNumberInCircle(bitmap, Number);
}
return bitmap; return bitmap;
} }
private void DrawNumber(Bitmap bitmap, int number) private void DrawDot(Bitmap bitmap, Color color)
{ {
using (var g = Graphics.FromImage(bitmap))
{
g.FillCircle(new SolidBrush(color), PixelSize / 2, PixelSize / 2, PixelSize / 2);
g.Flush();
}
}
private void DrawPixelBlock(Bitmap bitmap, Color color)
{
for (int x = 0; x < PixelSize; x++)
{
for (int y = 0; y < PixelSize; y++)
{
bitmap.SetPixel(x, y, color);
}
}
}
private void DrawNumber(Bitmap bitmap, int number, Color color)
{
using (var g = Graphics.FromImage(bitmap))
{
var font = g.GetIdealFontSize(PixelSize, "15", new Font("Arial", 8));
var fontColor = IdealTextColor(color);
g.DrawText(new SolidBrush(fontColor), font, PixelSize, StringAlignment.Center, number.ToString());
g.Flush();
}
}
private void DrawNumberInCircle(Bitmap bitmap, int number)
{
using (var g = Graphics.FromImage(bitmap)) using (var g = Graphics.FromImage(bitmap))
{ {
g.FillCircle(Brushes.White, PixelSize / 2, PixelSize / 2, NumberSize / 2); g.FillCircle(Brushes.White, PixelSize / 2, PixelSize / 2, NumberSize / 2);
@ -43,5 +76,14 @@ namespace LegoArtTool.BuildingInstruction
g.Flush(); g.Flush();
} }
} }
public Color IdealTextColor(Color bg)
{
int nThreshold = 105;
int bgDelta = Convert.ToInt32((bg.R * 0.299) + (bg.G * 0.587) + (bg.B * 0.114));
var foreColor = (255 - bgDelta < nThreshold) ? Color.Black : Color.White;
return foreColor;
}
} }
} }

View file

@ -27,7 +27,7 @@ namespace LegoArtTool.BuildingInstruction
} }
} }
public Bitmap GetBitmap() public Bitmap GetBitmap(bool useDots)
{ {
var bitmapSize = MatrixSize * BuildingInstructionElement.PixelSize; var bitmapSize = MatrixSize * BuildingInstructionElement.PixelSize;
var bitmap = new Bitmap(bitmapSize, bitmapSize); var bitmap = new Bitmap(bitmapSize, bitmapSize);
@ -37,7 +37,7 @@ namespace LegoArtTool.BuildingInstruction
{ {
for (int y = 0; y < MatrixSize; y++) for (int y = 0; y < MatrixSize; y++)
{ {
var pixelBitmap = Pixels[x, y].GetBitmap(); var pixelBitmap = Pixels[x, y].GetBitmap(useDots);
g.DrawImage(pixelBitmap, x * pixelBitmap.Width, y * pixelBitmap.Height); g.DrawImage(pixelBitmap, x * pixelBitmap.Width, y * pixelBitmap.Height);
} }
} }

View file

@ -9,10 +9,10 @@ namespace LegoArtTool.BuildingInstruction
{ {
public class BuildingInstructionService public class BuildingInstructionService
{ {
public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors) public Bitmap CreateBuildingInstructionImage(Bitmap bitmap, List<LegoArtColorInfo> legoArtColors, bool useDots)
{ {
var pixelMatrix = new BuildingInstructionMatrix(bitmap, legoArtColors); var pixelMatrix = new BuildingInstructionMatrix(bitmap, legoArtColors);
return pixelMatrix.GetBitmap(); return pixelMatrix.GetBitmap(useDots);
} }
public string PersistBuildingInstructions(Bitmap bitmap, string path) public string PersistBuildingInstructions(Bitmap bitmap, string path)