DrawString与图像中的轮廓

时间:2016-08-18 10:57:25

标签: winforms powershell

我如何画出轮廓字符串?例如:字符串白色的纯色,边框是黑色。我必须使用哪种方法来获得这种效果?也许有人知道一些例子。

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

function AddTextToImage {
    [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

    $srcImg = [System.Drawing.Image]::FromFile($sourcePath)

    $outputIImg = New-Object System.Drawing.Bitmap([int]($srcImg.width)),([int]($srcImg.height))

    $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))

    $Font = New-Object System.Drawing.Font("Bauhaus 93", 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

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

    $Image.DrawString($Title, $Font, $Brush, $rect, $format)

    $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 title d ias hdiusahdiusahdias hdisahidu sad sad sa ad asd asd asd sad sad asd as long"

cmd /c pause

1 个答案:

答案 0 :(得分:1)

您可以使用以下路径:

...
$Image.DrawString($Title, $Font, $Brush, $rect, $format)

$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)

$outputIImg.save($destPath, [System.Drawing.Imaging.ImageFormat]::jpeg)
...