Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active August 13, 2024 23:46
Show Gist options
  • Save vexx32/7a693af24dd5a732d029ace3eba75467 to your computer and use it in GitHub Desktop.
Save vexx32/7a693af24dd5a732d029ace3eba75467 to your computer and use it in GitHub Desktop.
quick example for system.drawing text to an image
if (-not ('System.Drawing.Bitmap' -as [type])) {
Add-Type -AssemblyName System.Drawing
}
[System.Drawing.Bitmap] $Image = [System.Drawing.Bitmap]::new(100, 100)
$Image.SetResolution(96, 96)
[System.Drawing.Graphics] $DrawingSurface = [System.Drawing.Graphics]::FromImage($Image)
$DrawingSurface.PageScale = 1.0
$DrawingSurface.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::AntiAlias
$DrawingSurface.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::ClearTypeGridFit
$Color = [System.Drawing.Color]::Red
$Brush = [System.Drawing.SolidBrush]::new($Color)
$Text = "123"
$Font = [System.Drawing.Font]::new("Arial", 32, "Regular", [System.Drawing.GraphicsUnit]::Pixel)
$DrawingSurface.DrawString($Text, $Font, $Brush, 0, 0)
$DrawingSurface.Flush()
$Image.Save($Path) # or save to a stream, etc., to do things in memory; check overloads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment