I am working on a PowerShell script to make images of characters (sort of like flashcards) in bulk.
And I am running into an issue I have seen before, and that is characters look very dirty. For example:
The large character looks good, but I believe this is only because of its size. If I enlarge the text on the right it looks similarly good. But I want the text to be this size.
I have used the following two methods to try and clean up the text, to no effect:
$grph.SmoothingMode.value__ClearTypeGridFit
The other approach that I have used:
$grph.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$grph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$grph.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
I believe this is more of an artifact from cleartype fonts than a code issue, but I am looking for help.
The complete script:
$List | ForEach-Object { $Name = $_.Name $Out = "$Path\$Name.png" $imgWidth = 270 $imgHeight = 450 $img = New-Object System.Drawing.Bitmap $imgWidth, $imgHeight $fontK = New-Object System.Drawing.Font "Meiryo", 85 $FontAll = New-Object System.Drawing.Font Meiryo, 35 $BrushBG = [System.Drawing.Brushes]::Transparent $BrushFG = [System.Drawing.Brushes]::Lime $grph = [System.Drawing.Graphics]::FromImage($img) $grph.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality $grph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $grph.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias $grph.FillRectangle($BrushBG,0,0,$img.Width,$img.Height) $kanji = $_.kanji $grph.DrawString([String]$kanji, [System.Drawing.Font]$fontK, $BrushFG, [int]0,[int]0) $kun = $_.kunyomi $fontOn = New-Object System.Drawing.Font "Meiryo", 36 $kunWidth = [System.Windows.Forms.TextRenderer]::MeasureText($kun, $fontOn) $kunLeft = $imgWidth - $kunWidth.Width $grph.DrawString([String]$kun, [System.Drawing.Font]$fontOn, $BrushFG, [int]$kunLeft,[int]0) $pKun = $_.pronounceKun $pKunSize = [System.Windows.Forms.TextRenderer]::MeasureText($pKun, $fontOn) $pKunLeft = $imgWidth - $pKunSize.Width $grph.DrawString([String]$pKun, [System.Drawing.Font]$fontOn, $BrushFG, $pKunLeft,$kunWidth.Height) $grph.Dispose() $img.Save($Out) Write-Output $Name, $ony }
I removed the code for parts of the image not shown, but it is essentially duplicate code.
How can I make the hiragana ひ and the "hi" not have theat black jagged outline?
I found what I needed to do.
I needed to use the System.Drawing.Text.TextRenderingHint
to add the clarity I wanted.
I played with removing the other sampling / aliasing information and it appeared with it I maintained a slightly better image.
The code for quality is now:
$grph = [System.Drawing.Graphics]::FromImage($img)
$grph.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$grph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$grph.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
$grph.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAlias
And the output looks like: