例如,我有cyrilic类型的词:“Кириллица”
如何在PowerShell脚本中使用它?我想在drawString
函数中使用此字符串。
我得到一些不正常的字符......
代码:
$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
)
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"
$img_rectangle = New-Object Drawing.Rectangle 0, 0, $srcImg.Width, $srcImg.Height
$Image.DrawImage($srcImg, $img_rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))
Write-Verbose "Draw title: $Title"
$Font = new-object System.Drawing.Font("SansSerif", 200, "Bold","Pixel")
$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 outline
$path = New-Object System.Drawing.Drawing2D.GraphicsPath
$path.AddString($Title, $Font.FontFamily, $Font.Style, $Font.Size, $rect, $format)
$Image.DrawPath([System.Drawing.Pens]::Black, $path)
#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 ($swp_curr_dir + "\image.jpg") -destPath ($swp_curr_dir + "\output.jpg") -Title "Кириллица"
cmd /c pause
在这段代码中,我正在尝试输出带有绘制的cyrilic字符串的新图像。
一开始,可以在我们的powershell脚本所在的目录中找到image.jpg。我只是通过鼠标右键单击运行此脚本。执行代码后,会出现带有居中文本的新附加图像。文字不是可读格式。
解决方案:Scrip.ps1如果以utf-8编码格式保存,则必须包含BOM。