我正在尝试在垂直中间的图像上绘制字符串,但我不知道如何从该文本的左右填充。如果我有一个非常大的字符串,它不适合图像,而不是新的行......
我现在有第二个PowerShell代码:
$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
答案 0 :(得分:2)
我稍微改变了你的例子:
[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"
关键是用不同的参数调用DrawString。