使用西里尔字符串

时间:2016-08-22 15:36:47

标签: powershell character-encoding powershell-v2.0 powershell-v3.0

例如,我有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。我只是通过鼠标右键单击运行此脚本。执行代码后,会出现带有居中文本的新附加图像。文字不是可读格式。

  1. 原始图片链接:https://i.imgsafe.org/b2ff1cb235.jpg
  2. 输出 图片链接:https://i.imgsafe.org/b2ff23d8f9.jpg
  3. 解决方案:Scrip.ps1如果以utf-8编码格式保存,则必须包含BOM。

    来自用户https://stackoverflow.com/users/4003407/petseral的正确答案

0 个答案:

没有答案