Vital Vital - 3 months ago 15
PowerShell Question

POWERSHELL: $Image.DrawString centering string with left and right padding

I'm trying draw string on image in vertical middle, but I don't know how do padding from left and right of this text. If I have very big string, it does not fit on image, not making new line...

I have now second powershell code:

$swp_curr_dir = split-path -parent $MyInvocation.MyCommand.Definition
[void][reflection.assembly]::loadwithpartialname("system.windows.forms")

Function AddTextToImage {
# Orignal code from http://www.ravichaganti.com/blog/?p=1012
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)][String] $sourcePath,
[Parameter(Mandatory=$true)][String] $destPath,
[Parameter(Mandatory=$true)][String] $Title,
[Parameter()][String] $Description = $null
)

Write-Verbose "Load System.Drawing"
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

Write-Verbose "Get the image from $sourcePath"
$srcImg = [System.Drawing.Image]::FromFile($sourcePath)

Write-Verbose "Create a bitmap as $destPath"
$outputIImg = new-object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

Write-Verbose "Intialize Graphics"
$Image = [System.Drawing.Graphics]::FromImage($outputIImg)
$Image.SmoothingMode = "AntiAlias"

$Rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
$Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))

Write-Verbose "Draw title: $Title"
$Font = new-object System.Drawing.Font("Bauhaus 93", 130, "Bold","Pixel")

#get font size
$font_size = [System.Windows.Forms.TextRenderer]::MeasureText($Title, $Font)
$font_size_width = $font_size.Width
$font_size_height = $font_size.Height

# calc text in middle
$text_in_middle_top_offset = ($srcImg.Height - $font_size_height) / 2
$text_in_middle_left_offset = ($srcImg.Width - $font_size_width) / 2

#styling font
$Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 255, 255, 255))

#lets draw font
$Image.DrawString($Title, $Font, $Brush, $text_in_middle_left_offset, $text_in_middle_top_offset)

Write-Host $text_in_middle_left_offset



Write-Verbose "Save and close the files"
$outputIImg.save($destPath, [System.Drawing.Imaging.ImageFormat]::jpeg)
$outputIImg.Dispose()
$srcImg.Dispose()
}

AddTextToImage -sourcePath ($swp_curr_dir + "\image.jpg") -destPath ($swp_curr_dir + "\output.jpg") -Title "Some long long long long long long long long long long long long string here"

cmd /c pause

Answer

I've changed your example slightly:

[void][reflection.assembly]::loadwithpartialname("system.windows.forms")

Function AddTextToImage {
    # Orignal code from http://www.ravichaganti.com/blog/?p=1012
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true)][String] $sourcePath,
        [Parameter(Mandatory=$true)][String] $destPath,
        [Parameter(Mandatory=$true)][String] $Title,
        [Parameter()][String] $Description = $null
    )

    Write-Verbose "Load System.Drawing"
    [Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

    Write-Verbose "Get the image from $sourcePath"
    $srcImg = [System.Drawing.Image]::FromFile($sourcePath)

    Write-Verbose "Create a bitmap as $destPath"
    $outputIImg = new-object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

    Write-Verbose "Intialize Graphics"
    $Image = [System.Drawing.Graphics]::FromImage($outputIImg)
    $Image.SmoothingMode = "AntiAlias"

    $Rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
    $Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))

    Write-Verbose "Draw title: $Title"
    $Font = new-object System.Drawing.Font("Bauhaus 93", 130, "Bold","Pixel")

    #get font size
    $font_size = [System.Windows.Forms.TextRenderer]::MeasureText($Title, $Font)
    $font_size_width = $font_size.Width
    $font_size_height = $font_size.Height

    $rect = [System.Drawing.RectangleF]::FromLTRB(0, 0, $srcImg.Width, $srcImg.Height)
    $format = [System.Drawing.StringFormat]::GenericDefault
    $format.Alignment = [System.Drawing.StringAlignment]::Center
    $format.LineAlignment = [System.Drawing.StringAlignment]::Center

    #styling font
    $Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::FromArgb(255, 255, 255, 255))

    #lets draw font
    $Image.DrawString($Title, $Font, $Brush, $rect, $format)

    Write-Verbose "Save and close the files"
    $outputIImg.save($destPath, [System.Drawing.Imaging.ImageFormat]::jpeg)
    $outputIImg.Dispose()
    $srcImg.Dispose()
}

AddTextToImage -sourcePath D:\Temp\Koala.jpg -destPath D:\Temp\Koala2.jpg -Title "Some long long long long long long long long long long long long string here"

The key is to call DrawString with different arguments.

Comments