'Aligning text within a graphic with Powershell
I successfuly cobbled together the following code to generate as servername jpg to be used as a desktop background. What I can't figure out from the code is why I have to create the Graphic from the line:
$Graphic = [System.Drawing.Graphics]::FromImage($Bitmap)
and then fill the rectangle with the information
If I run the line:
$Graphic.DrawString($ENV:COMPUTERNAME,$Font,$FGColour,$Rectangle,$Format)
using :
$Graphic.DrawString($ENV:COMPUTERNAME,$Font,$FGColour,$Bitmap,$Format)
I get the error:
Cannot find an overload for "DrawString" and the argument count: "5".
At line:41 char:1
+ $Graphic.DrawString($ENV:COMPUTERNAME,$Font,$FGColour,$Bitmap,$Format
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Add-Type -AssemblyName System.Drawing
$Path = ("C:\Users\Public\Pictures")
$File = "$Path\ServerName.jpg"
$Rectangle = [System.Drawing.RectangleF]::FromLTRB(0, 15, 1060,150)
$Bitmap = new-object System.Drawing.Bitmap 1060,150
$Font = new-object System.Drawing.Font Courier, 90
$BGColour = [System.Drawing.Brushes]::SteelBlue
$FGColour = [System.Drawing.Brushes]::DarkSlateGray
$Graphic = [System.Drawing.Graphics]::FromImage($Bitmap)
$Graphic.FillRectangle($BGColour,0,0,$Rectangle.Width,($rectangle.Height)+15)
$Format = [System.Drawing.StringFormat]::GenericDefault
$Format.Alignment = [System.Drawing.StringAlignment]::Center
$Format.LineAlignment = [System.Drawing.StringAlignment]::Center
$Graphic.DrawString($ENV:COMPUTERNAME,$Font,$FGColour,$Rectangle,$Format)
$Graphic.Dispose()
$Bitmap.Save($File)
Invoke-Item $File
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
