2

我想繪製垂直中間的圖像上的字符串,但我不知道如何從這個文本的左側和右側填充。如果我有非常大的字符串,它不適合於圖像,而不是製造新的行...

我現在第二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 

回答

1

我已經改變了小幅你的例子:

[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

+0

非常感謝你:)幫我。最後一個問題:垂直如何對齊中間?因爲現在文本坐在頂部 – Vital

+0

$ format.LineAlignment = [System.Drawing.StringAlignment] :: Center – Vital

+0

格式參數中有'LineAlignment'屬性。我已經更新了答案。 –